K

@kenbitanga

Ruby: Lambda Counter

Ruby
2 years ago
def counter(start=0) lambda { start += 1 } end count = counter() puts count.call puts count.call puts count.call

Ruby: Passing a Block vs Proc vs Lambda

Ruby
2 years ago
def fun yield if block_given? end prox = proc { puts 'proc' } lamb = lambda { puts 'lambda' } lamd = -> () { puts 'lambda shorthand' } fun() { puts 'block' } fun(&prox)

Ruby: Callbacks Using Procs

Ruby
2 years ago
def compose(x, proc1, proc2) proc2.call(proc1.call(x)) end double = proc { |x| x * 2 } square = lambda { |x| x * x } triple = -> (x) { x * 3 } p compose(5, double, square) p compose(5, square, double)

Ruby: Extending Integer Class

Ruby
2 years ago
class Integer def is_odd if self % 2 != 0 return true end return false end end p [1,2,3,4,5].filter(&:is_odd)

Ruby: Block vs Proc vs Lambda vs Lambda Shorthand ( -> () {} )

Ruby
2 years ago
def greet(name) yield name end greet('calling a block using yield') { |x| puts x } def greet(name, &block) block.call(name) end

Python: Callbacks

Python
2 years ago
def compose(proc1, proc2): def wrapper(x): return proc2(proc1(x)) return wrapper def double(x): return x * 2 def square(x): return x * x

JS: Callbacks

NodeJS
2 years ago
const compose = (proc1, proc2) => (x) => proc2(proc1(x)); const double = (x) => x * 2; const square = (x) => x * x; doubleThenSquare = compose(double, square) squareThenDouble = compose(square, double) console.log(doubleThenSquare(5))

Ruby: Method Returning a Proc

Ruby
2 years ago
def compose proc1, proc2 Proc.new do |x| proc2.call(proc1.call(x)) end end double = Proc.new do |x| x * 2 end

JS: Fibonacci - Generator Function

NodeJS
2 years ago
function* fib() { let a = 0, b = 1; while (true) { yield b; [a, b] = [b, a + b]; } } for (const i of fib()) { if (i > 55) break;

Ruby: Fibonacci - Enumerator.produce

Ruby
2 years ago
fib = Enumerator.produce([0, 1]) { |a, b| [b, a + b] } print fib.take(10).map(&:first)

Ruby: Fibonacci - Enumerator (Generator in Python)

Ruby
2 years ago
fib = Enumerator.new do |enum| a, b = 0, 1 loop do enum.yield b a, b = b, a+b end end print fib.take(10)

JS: Convert Array to Object - Reversible Key-Value - Callback Filter Function

NodeJS
2 years ago
const arr2obj = (arr, reverse = false, isValid = (x) => { return true }) => { let obj = {} arr.forEach((v, i) => { if (!(isValid(i))) return reverse ? obj[v] = i : obj[i] = v }) return obj }

JS: Convert Array to Object

NodeJS
2 years ago
a = Array.from('abcde') console.log('Input array') console.log(a) console.log('Using .reduce') b = a.reduce((a, c, i) => { if (i % 2 !== 0) return a return { ...a, [c]: i } }, {}) console.log(b)

JS: Zip Arrays

NodeJS
2 years ago
const zip = (...arrs) => Array(Math.max(...arrs.map(a => a.length))) .fill() .map((_, i) => arrs.map(a => a[i]) ) const zap = (...arrs) => Array.from( Array(Math.max(...arrs.map(a => a.length))), (_, i)

Java: Fibonacci - Recursive - Memoized - HashMap

Java
2 years ago
import java.util.*; import java.lang.*; import java.io.*; class Main { private static int fib(int n) { if (n < 1) { return 0; } if (n < 3) { return 1; } HashMap<Integer, Integer> memo = new HashMap<>();

CSharp: Fibonacci - Recursive - Memoized - Dictionary

C#
2 years ago
using System; using System.Collections.Generic; namespace MyCompiler { class Program { private static int Fib(int n) { if (n < 1) { return 0; }

Go: Fibonacci - Recursive - Memoized

Go
2 years ago
package main import "fmt" func fib(n int) int { if n < 1 { return 0 } if n < 3 { return 1 } memo := make(map[int]int)

JS: Fibonacci - Memoized

NodeJS
2 years ago
function fib(n) { if (n < 1) return 0 if (n < 3) return 1 const memo = new Map() if (!memo.has(n)) { memo.set(n, fib(n-1) + fib(n-2)) }

JS: Fibonacci - Memoized - Decorated

NodeJS
2 years ago
function fib(n) { if (n < 1) return 0 if (n < 3) return 1 return fib(n-1) + fib(n-2) } // decorator function function memoize(func) { const memo = new Map()

JS: Factorial - Recursive - Memoized

NodeJS
2 years ago
function fact(n) { const memo = new Map() if (n < 0) return undefined if (n < 2) return 1 if (!memo.has(n)) { memo.set(n, n * fact(n-1)) }