Skip to content

Instantly share code, notes, and snippets.

@elzup
Last active May 22, 2023 17:02
Show Gist options
  • Save elzup/886e47e2f13d00e4407b8efe5e71eb81 to your computer and use it in GitHub Desktop.
Save elzup/886e47e2f13d00e4407b8efe5e71eb81 to your computer and use it in GitHub Desktop.
Goal: 100 Arrow functions, Anonymous function, lambda, closure example.
...
checks
// no args
// block
// implicit parameter
// assign
const list = [1, 2, 3]
list.map(v => v * v)
list.map(function (v) { return v })
// [ 1, 4, 9 ]
// no arg
list.map(() => 0)
// block
list.map(v => {
return 0
})
// implicit parameter
list.map(() => arguments[0] * arguments[0])
// assign
const add = (a, b) => a + b
add(1, 2)
list(map(lambda v: v * v, [1, 2, 3]))
# [1, 4, 9]
# no args
map(lambda: 0, [1, 2, 3])
# block: none
# implicit parameter: none
# assign
add = lambda a, b: a + b
add(1, 2)
[1, 2, 3].map { |v| v * v }
# [1, 4, 9]
# no args
[1, 2, 3].map { 0 }
# block
[1, 2, 3].map do
0
end
# implicit parameter: none
# assign
add = ->(a, b) { a + b }
add.call(1, 2)
add.(1, 2)
double = ->(v) { v * 2 }
[11, 22].map &double
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3);
Stream<Integer> res0 = list.stream().map(v -> v * v);
// no args: none
// block
Stream<Integer> res1 = list.stream().map(v -> {
return 0;
});
// implicit parameter: none
// assign
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
// add.apply(1, 2)
}
}
val list = listOf(1, 2, 3)
list.map { v -> v * v }
// [1, 4, 9]
// no args
list.map { 0 }
// block: none
// implicit parameter
list.map { it * it }
// assign
val add = { x: Int, y: Int -> x + y }
add(1, 2)
<?php
array_map(fn($v) => $v * $v, [1, 2, 3]);
// [1,4,9]
// no args
array_map(fn() => 0, [1, 2, 3]);
// block
array_map(function($v) {
return 0;
}, [1, 2, 3]);
// implicit parameter: none
// assign
$add = fn($a, $b) => $a + $b;
$add(1, 2);
[1, 2, 3].map { (v) -> Int in v * v }
// [1, 4, 9]
// no arg: none
// block
[1, 2, 3].map { (v) -> Int in
return 0
}
// implicit parameter
[1, 2, 3].map { $0 * $0 }
// assign
let add = { (a: Int, b: Int) -> Int in a + b }
let add2: (Int, Int) -> Int = { (a, b) in a + b }
let add3: (Int, Int) -> Int = { $0 + $1 }
let add4: (Int, Int) -> Int
add4 = { $0 + $1 }
add(1, 2)
[1, 2, 3].map(v => v * v)
[1, 2, 3].map((v: number): number => v * v)
// [ 1, 4, 9 ]
// no arg
[1, 2, 3].map(() => 0)
// block
[1, 2, 3].map(v => {
return 0
})
// implicit parameter: none
// assign
const add = (a: number, b: number): number => a + b
const add2: (a: number, b: number) => number = (a, b) => a + b
add(1, 2)
[1, 2, 3].map (v) -> v * v
[1, 2, 3].map (v) => v * v
# [ 1, 4, 9 ]
# no arg
[1, 2, 3].map -> 0
# block
[1, 2, 3].map ->
0
# implicit parameter: none
# assign
add = (a, b) -> a + b
add 1, 2
[1, 2, 3].map (v) -> v * v
# [ 1, 4, 9 ]
# no arg
[1, 2, 3].map -> 0
# block
[1, 2, 3].map ->
0
# implicit parameter
[1, 2, 3].map (-> it * it)
[1, 2, 3].map (-> @@0 * @@0)
# assign
add = (a, b) -> a + b
add 1, 2
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> list{ 1, 2, 3 };
auto res = vector<int>(3);
transform(list.begin(), list.end(), res.begin(), [](int x){return x * x;});
// no args: none
// block
transform(list.begin(), list.end(), res.begin(), [](int x){
return 0;
});
// implicit parameter: none
// assign
auto add = [](int a, int b) { return a + b; };
auto add2 = [](int a, int b) -> int { return a + b; };
add(1, 2);
}
package main
func call(v int, callback func(int) int) int { return callback(v) }
func main() {
call(3, func(a int) int { return a * a })
// omit args: none
// block
call(3, func(a int) int {
return 0
})
// implicit parameter: none
// assign
add := func(x int, y int) int { return x + y }
add(1, 2)
}
fn main() {
let nums = vec![1, 2, 3];
let res1: Vec<i32> = nums.iter().map(|v| v * v).collect();
// [1, 4, 9]
// no args
let res2: Vec<i32> = nums.iter().map(|_| 0).collect();
// [0, 0, 0]
// block
let res3: Vec<i32> = nums.iter().map(|v| {
v * 2
}).collect();
// [2, 4, 6]
// implicit parameter: none
// https://github.com/rust-lang/rfcs/issues/2554
// assign
let triple = |v| v * 3;
let res5: Vec<i32> = nums.iter().map(triple).collect();
// [3, 6, 9]
}
my @list = (1, 2, 3);
$,=',';
$\="\n";
map { $_ * $_ } @list;
# 1,4,9
# no args
map { 0 } @list;
# block
map {
$_;
} @list;
# implicit parameter
map { $_ * $_ } @list;
# assign
my $add = sub { $_[0] + $_[1] };
$add->(1, 2);
list = [1, 2, 3]
# IO.inspect
list |> Enum.map(fn v -> v * v end)
# [1, 4, 9]
# no args: none
list |> Enum.map(fn _ -> 0 end)
# block
list |> Enum.map(fn _ ->
0
end)
# implicit parameter
list |> Enum.map(&(&1 * &1))
# assign
add = fn (a, b) -> a + b end
add.(1, 2)
list = [1, 2, 3] :: [Int]
main = do
map (\v -> v * v) list
-- [ 1, 4, 9 ]
-- no args: none
-- block: none
-- implicit parameter
map (^2) list;
-- assign
let add a b = a + b
add 1 2
object Main {
def main(args: Array[String]): Unit = {
val list = Seq(1, 2, 3)
list.map((v) => v * v)
// List(1, 4, 9)
// no args: none
println(list.map((_) => 0))
// block: none
// implicit parameter
list.map(_ * 2)
// List(2, 4, 6)
// assign
var add = (a: Int, b: Int) => a + b
add(1, 2)
// 3
}
}
-- _ = require 'underscore' -- Lua doesn't have HOC, get from lib
local list = {1, 2, 3}
_.map(list, function(v) return v * v end)
-- [ 1, 4, 9 ]
-- no arg
_.map(list, function() return 0 end)
-- block
_.map(list, function()
return 0
end
)
-- implicit parameter: none
-- assign
local add = function(a, b) return a + b end
print(add(1, 2))
final list = [1, 2, 3];
void main() {
list.map((v) => v * v).toList();
// [ 1, 4, 9 ]
// no arg: none
// block: none
// implicit parameter: none
// assign
final add = (a, b) => a + b;
add(1, 2);
}
my @list = 1..3;
say @list.map(-> $a { $a * $a });
# (1 4 9)
# no arg
say @list.map({ 0 });
# block
say @list.map(-> $a {
1;
0
});
# implicit parameter
say @list.map({$_ * $_});
# (1 4 9)
# assign
my &add = -> $a, $b { $a + $b };
my &add2 = { $^a + $^b };
say &add(1, 2);
# 3
list = [1 to 3]
list.map (v) -> v * v
# [1,4,9]
# no arg
list.map -> 0
# block
list.map ->
1
0
# implicit parameter
list.map -> &0 * &0
# [1,4,9]
# assign
add = (a, b) -> a + b
add(1, 2)
list = [1 to 3]
list = 1:3
map(x-> x * x, list)
# Vector{Int64}:
# 1
# 4
# 9
# no arg: none
map(_ -> 0, list)
# block
map(v -> begin
0
v * v
end, list)
map(list) do v
1
2
end
# implicit parameter: none
# assign
add = (a, b) -> a + b
add(1, 2)
import std/sugar
import sequtils
let list = @[1, 2, 3]
echo map(list, (x) => x * x)
echo list.map (x) => x * x
# @[1, 4, 9]
# no arg: none
let na = proc: int = 0
# block
echo list.map (x) =>
#
x * x
# implicit parameter: none
# assign
let add = (a: int, b: int) => a + b
echo add(1, 2)
#
# without suger
import sequtils
let list = @[1, 2, 3]
map(list, proc(x: int): int = x * x)
# @[1, 4, 9]
list.map do (x:int) -> int : x * x
# no arg: none
map(list, proc(x: int): int = 0)
# block
map(list, proc(x: int): int =
#
result = x * x
)
# implicit parameter: none
# assign
let add = proc(a: int, b: int): int = a + b
add(1, 2)
import std/sugar
import sequtils
list = [1, 2, 3]
map (x: "foo" + x) [ "bar" "bla" "abc" ]
echo map(list, (x) => x * x)
echo list.map (x) => x * x
# @[1, 4, 9]
# no arg: none
let na = proc: int = 0
# block
echo list.map (x) =>
#
x * x
# implicit parameter: none
# assign
let add = (a: int, b: int) => a + b
echo add(1, 2)
#
# without suger
import sequtils
let list = @[1, 2, 3]
map(list, proc(x: int): int = x * x)
# @[1, 4, 9]
list.map do (x:int) -> int : x * x
# no arg: none
map(list, proc(x: int): int = 0)
# block
map(list, proc(x: int): int =
#
result = x * x
)
# implicit parameter: none
# assign
add = { a, b }: a + b
add { a="hello"; b="world"; }
@elzup
Copy link
Author

elzup commented Feb 27, 2020

  • TypeScript
  • CoffeeScript
  • Elm *
  • Rust *
  • D *
  • C++ *
  • Go *
  • ShellScript
  • C# *
  • Coco *
  • Kotlin *
  • Swift *
  • Elixir *
  • Perl *
  • Haskell *
  • PureScript
  • Dart *
  • awk
  • Scala

* I'm biginner, REFACTOR ME

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