0% found this document useful (0 votes)
3K views2 pages

Next Item Function Implementation

Uploaded by

Elliot Lassey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views2 pages

Next Item Function Implementation

Uploaded by

Elliot Lassey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Next Item Function Implementation

Given a sequence of items and a specific item in that sequence, return the item immediately
following the item specified. If the item occurs more than once in a sequence, return the item
after the first occurence. This should work for a sequence of any type.

When the item isn't present or nothing follows it, the function should return nil in Clojure and
Elixir, Nothing in Haskell, undefined in JavaScript, None in Python.

next_item([1, 2, 3, 4, 5, 6, 7], 3) # => 4


next_item(['Joe', 'Bob', 'Sally'], 'Bob') # => "Sally"

Here’s how you can implement the `next_item` function in various programming languages as
described:

Python

python

def next_item(seq, item):


try:
idx = seq.index(item)
return seq[idx + 1] if idx + 1 < len(seq) else None
except ValueError:
return None

# Examples
print(next_item([1, 2, 3, 4, 5, 6, 7], 3)) # => 4
print(next_item(['Joe', 'Bob', 'Sally'], 'Bob')) # => 'Sally'

JavaScript

javascript

function next_item(seq, item) {


const idx = seq.indexOf(item);
return (idx !== -1 && idx + 1 < seq.length) ? seq[idx + 1] : undefined;
}

// Examples
console.log(next_item([1, 2, 3, 4, 5, 6, 7], 3)); // => 4
console.log(next_item(['Joe', 'Bob', 'Sally'], 'Bob')); // => 'Sally'

Clojure

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/2
clojure

(defn next-item [seq item]


(let [idx (.indexOf seq item)]
(if (and (not= idx -1) (< (inc idx) (count seq)))
(nth seq (inc idx))
nil)))

;; Examples
(next-item [1 2 3 4 5 6 7] 3) ;; => 4
(next-item ["Joe" "Bob" "Sally"] "Bob") ;; => "Sally"

Elixir

elixir

defmodule Sequence do
def next_item(seq, item) do
case Enum.find_index(seq, fn x -> x == item end) do
nil -> nil
idx -> Enum.at(seq, idx + 1, nil)
end
end
end

# Examples
IO.inspect Sequence.next_item([1, 2, 3, 4, 5, 6, 7], 3) # => 4
IO.inspect Sequence.next_item(["Joe", "Bob", "Sally"], "Bob") # => "Sally"

Haskell

haskell

nextItem :: Eq a => [a] -> a -> Maybe a


nextItem xs item =
case dropWhile (/= item) xs of
(_:x:_) -> Just x
_ -> Nothing

-- Examples
-- nextItem [1, 2, 3, 4, 5, 6, 7] 3 -- => Just 4
-- nextItem ["Joe", "Bob", "Sally"] "Bob" -- => Just "Sally"

Each function checks if the item is in the sequence and retrieves the item after its first occurrence. If
the item isn't present or is the last in the sequence, it returns `nil`, `None`, `undefined`, or `Nothing`
as per the language's behavior.
Is this conversation helpful so far?
ChatGPT can make mistakes. Check important info.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/2

You might also like