K

@kenbitanga

Elixir: Enum Methods

Python
2 years ago
nums = [1, 2, 3, 4, 5] Enum.map(nums, fn n -> n * 2 end) # [2, 4, 6, 8, 10] Enum.filter(nums, fn n -> rem(n, 2) == 1 end)

Elixir: Factorial

Python
2 years ago
# Factorial in Elixir defmodule Factorial do def of(n) do case n do 0 -> 1 1 -> 1 n -> n * of(n - 1) end end

Go: Concurrency - Unbuffered Channel - Calculate Sum of Slice of Numbers

Go
2 years ago
package main import "fmt" func calcSum(s []int, ch chan<- int) { result := 0 for _, v := range s { result += v } ch <- result

Go: Concurrency - Buffered Channel - Calculate Sum of Slice of Numbers

Go
2 years ago
package main import "fmt" func calcSum(s []int, ch chan<- int) { result := 0 for _, v := range s { result += v } ch <- result

Go: Concurrency - Buffered Channel

Go
2 years ago
package main import ( "fmt" "time" ) func goroutine(ch chan<- string) { time.Sleep(time.Second * 1) ch <- "done"

Go: Learn Go Concurrency by Kantan Coding (YT)

Go
2 years ago
package main import ( "fmt" "time" ) func thisTakesForever(id int, ch chan<- string) { time.Sleep(time.Second * 2) ch <- "done"

Go: Generics

Go
2 years ago
package main import "fmt" type Number interface { int | int32 | int64 | float32 | float64 } func sumNumbers[T Number](numbers []T) T { var result T

Go: Blocking Nature of Channel

Go
2 years ago
package main import "fmt" func main() { // create channel ch := make(chan string) // function call with goroutine go receiveData(ch)

Ruby: How to pass a block, a proc, a lambda, a method, a static method

Ruby
2 years ago
my_lambda = ->(x) { print x } my_proc = proc { |x| print x } def my_method(x) print x end class MyClass def self.my_static_method(x)

Go: Channels - Unbuffered

Go
2 years ago
package main import "fmt" func main() { ch := make(chan int) go func(ch chan<- int) { fmt.Println("hello")

PHP: Generators - Fibonacci

PHP
2 years ago
<?php function fib() { [$x, $y] = [0, 1]; while (true) { yield $x; [$x, $y] = [$y, $x + $y]; } }

Go: Generator Function Using Channels

Go
2 years ago
package main import "fmt" func fib(ch chan<- int) { x, y := 0, 1 // infinite loop for { ch <- x

Go: Generate Fibonacci Sequence Using Unbuffered Channel

Go
2 years ago
package main import "fmt" func fib(n int, ch chan<- int) { x, y := 0, 1 for i := 0; i < n; i++ { ch <- x x, y = y, x+y }

Go: Implement Array Map, Filter Functions

Go
2 years ago
package main import "fmt" func Map(in []int, fn func(int) int) []int { out := []int{} for i := 0; i < len(in); i++ { out = append(out, fn(in[i])) } return out }

Ruby: Implement Array Reduce Method

Ruby
2 years ago
class Array def Reduce(acc = 0) each do |e| acc = yield(acc, e) end acc end end arr = [1,2,3,4,5]

Ruby: Implement Array Filter Method

Ruby
2 years ago
class Array def Filter result = [] each do |e| result << e if yield(e) end result end end

Ruby: Implement Array Map Method

Ruby
2 years ago
class Array def Map result = [] each do |e| result << yield(e) end result end end

Ruby: Yield vs Call - Passing Blocks in Various Ways

Ruby
2 years ago
def fun() "yielded #{yield}" end puts fun() { 'regular block ' } puts fun(&-> { 'block passed as argument' }) def fun(&block) "called #{block.call}" end

Ruby: Use a lambda as a closure to capture runtime variables

Ruby
2 years ago
# Example: Use a lambda as a closure to capture runtime variables def create_counter(start) lambda { start += 1 } end counter = create_counter(0) puts counter.call # Output: 1 puts counter.call # Output: 2

Ruby: Use a lambda for dynamic code generation

Ruby
2 years ago
# Example: Use a lambda for dynamic code generation def create_method(name, block) define_method(name, &block) end a_lambda = -> { puts "Hello, world!" } create_method(:hello, a_lambda) hello # Output: "Hello, world!"