JavaScript:
map = (fn, [x, ...xs]) => (x === undefined ? [] : [fn(x), ...map(fn, xs)])
Python:
map = lambda f, xs: [f(x) for x in xs]
Haskell:
class Delegated(object): | |
def __init__(self, delegated_name, attr): | |
self.attr_name = attr | |
self.delegated_name = delegated_name | |
def __get__(self, instance, owner): | |
if instance is None: | |
return self | |
else: | |
return getattr(self.delegate(instance), self.attr_name) |
import random | |
from random import choice, randint | |
from string import digits | |
# Default constructors for the ``random_list``. | |
default_constructors = ( | |
object, | |
dict, | |
lambda: bool(randint(0, 1)), | |
lambda: randint(1, 1000), |
(label eval (lambda (e a) | |
(cond | |
((atom e) (assoc e a) | |
((atom (car e)) | |
(cond | |
((eq (car e) quote) (cadr e)) | |
((eq (car e) atom) (atom (eval (cadr e) a))) | |
((eq (car e) eq) (eq (eval (card e a) | |
(eval (caddr e) a))) | |
((eq (car e) car) (car (eval (cadr e) a))) |
(defn reduce* | |
[f acc coll] | |
(if (empty? coll) | |
acc | |
(recur f (f acc (first coll)) (rest coll)))) |
(defn map* | |
[f coll] | |
(reduce* (fn [acc x] (conj acc (f x))) [] coll)) |
(defn filter* | |
[p coll] | |
(reduce* (fn [acc x] (if (p x) (conj acc x) acc)) [] coll)) |
map = (fn, [head, ...tail]) => (head === undefined ? [] : [fn(head), ...map(fn, tail)]) |
JavaScript:
map = (fn, [x, ...xs]) => (x === undefined ? [] : [fn(x), ...map(fn, xs)])
Python:
map = lambda f, xs: [f(x) for x in xs]
Haskell:
[:div | |
[:button#counter-btn | |
{:class "btn active" | |
:style {:padding 5} | |
:on-click (fn [e] (swap! counter inc))} | |
"Click me!"]] |
<div> | |
<button class="btn active" | |
id="counter-btn" | |
style="padding: 5px;"> | |
Click me! | |
</button> | |
</div> | |
<!-- add also JavaScript code for | |
the event listener --> |