Last active
May 23, 2020 13:46
-
-
Save danilobellini/7998775 to your computer and use it in GitHub Desktop.
Simple closure example in Scheme, Haskell, Smalltalk, Python, Ruby, Clojure, CoffeeScript and JavaScript/ECMAScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Clojure | |
(defn mul [value] | |
(fn [data] | |
(map | |
(fn [item] (* item value)) | |
data))) | |
; user=> ((mul 2) [3 2 8 -2]) | |
; (6 4 16 -4) | |
; user=> ((mul 5) [1 -1 0 2 3]) | |
; (5 -5 0 10 15) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CoffeeScript | |
mul = (value) -> (data) -> data.map (item) -> item * value | |
# coffee> (mul 2) [3, 2, 8, -2] | |
# [ 6, 4, 16, -4 ] | |
# coffee> (mul 5) [1, -1, 0, 2, 3] | |
# [ 5, | |
# -5, | |
# 0, | |
# 10, | |
# 15 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Haskell | |
let mul v = (\d -> map (* v) d) | |
-- > mul 2 [3, 2, 8, -2] | |
-- [6,4,16,-4] | |
-- > mul 5 [1, -1, 0, 2, 3] | |
-- [5,-5,0,10,15] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript / ECMAScript | |
var mul = function(value){ | |
return function(data){ | |
return data.map(function(item){ return item * value; }); | |
}; | |
}; | |
// > mul(2)([3, 2, 8, -2]) | |
// [ 6, 4, 16, -4 ] | |
// > mul(5)([1, -1, 0, 2, 3]) | |
// [ 5, | |
// -5, | |
// 0, | |
// 10, | |
// 15 ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python | |
def mul(value): | |
return lambda data: map(lambda item: item * value, data) | |
# >>> mul(2)([3, 2, 8, -2]) | |
# [6, 4, 16, -4] | |
# >>> mul(5)([1, -1, 0, 2, 3]) | |
# [5, -5, 0, 10, 15] | |
# PS: In Python 3.x the result value is lazy-evaluated (not a list). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby | |
def mul(value) | |
lambda{|data| data.map {|item| item * value}} | |
end | |
# irb(main):004:0> (mul 2).call [3, 2, 8, -2] | |
# => [6, 4, 16, -4] | |
# irb(main):005:0> (mul 5).call [1, -1, 0, 2, 3] | |
# => [5, -5, 0, 10, 15] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; Scheme | |
(define (mul value) | |
(lambda (data) | |
(map | |
(lambda (item) (* item value)) | |
data))) | |
; > ((mul 2) (list 3 2 8 -2)) | |
; (6 4 16 -4) | |
; > ((mul 5) (list 1 -1 0 2 3)) | |
; (5 -5 0 10 15) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Smalltalk" | |
mul := [:value | | |
[:data | | |
data collect: [:item | value * item]. | |
]. | |
]. | |
"st> (mul value: 2) value: #(3 2 8 -2)." | |
"(6 4 16 -4 )" | |
"st> (mul value: 5) value: #(1 -1 0 2 3)." | |
"(5 -5 0 10 15 )" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python (no lambdas) | |
def mul(value): | |
def func(data): | |
def item_mul(item): | |
return item * value | |
return map(item_mul, data) | |
return func | |
# >>> mul(2)([3, 2, 8, -2]) | |
# [6, 4, 16, -4] | |
# >>> mul(5)([1, -1, 0, 2, 3]) | |
# [5, -5, 0, 10, 15] | |
# PS: In Python 3.x the result value is lazy-evaluated (not a list). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment