Skip to content

Instantly share code, notes, and snippets.

@IwoHerka
Created June 28, 2019 10:56
Show Gist options
  • Save IwoHerka/4dc3b43fba09eb7b8751f5832dbfcc57 to your computer and use it in GitHub Desktop.
Save IwoHerka/4dc3b43fba09eb7b8751f5832dbfcc57 to your computer and use it in GitHub Desktop.

JavaScript:

map = (fn, [x, ...xs]) => (x === undefined ? [] : [fn(x), ...map(fn, xs)])

Python:

map = lambda f, xs: [f(x) for x in xs]

Haskell:

map _ [] = []
map f (x:xs) = f x : map f xs

Clojure:

(defn map
  [f coll]
  (reduce (fn [acc, x] (conj acc (f x))) [] coll))

Kotlin

fun <A, B> map(f: (A) -> B, coll: List<A>) = arrayListOf<B>().apply {
    coll.forEach { add(f(it)) }
}

Ruby:

def map(coll, &fn)
    acc = []

    coll.each do |x|
      acc << fn.call(x)
    end

    acc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment