diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-07-03 20:45:50 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-07-03 20:45:50 +0100 |
| commit | 24658259254bc705c627ba11b73b8587456ad2e4 (patch) | |
| tree | f1f104123e095a1ef71bc77957c0a84b662df19b | |
| parent | 7c87d609ea4e6e5d854a52825baae1b9d032bc5e (diff) | |
| download | perlweeklychallenge-club-24658259254bc705c627ba11b73b8587456ad2e4.tar.gz perlweeklychallenge-club-24658259254bc705c627ba11b73b8587456ad2e4.tar.bz2 perlweeklychallenge-club-24658259254bc705c627ba11b73b8587456ad2e4.zip | |
- Added guest contributions by Laurent Rosenfeld.
| -rw-r--r-- | challenge-171/laurent-rosenfeld/d/ch-2.d | 13 | ||||
| -rw-r--r-- | challenge-171/laurent-rosenfeld/julia/ch-2.jl | 13 | ||||
| -rw-r--r-- | challenge-171/laurent-rosenfeld/kotlin/ch-2.kt | 11 | ||||
| -rw-r--r-- | challenge-171/laurent-rosenfeld/nim/ch-2.nim | 10 | ||||
| -rw-r--r-- | challenge-171/laurent-rosenfeld/python/ch-2.py | 11 | ||||
| -rw-r--r-- | challenge-171/laurent-rosenfeld/ruby/ch-2.rb | 10 | ||||
| -rw-r--r-- | challenge-171/laurent-rosenfeld/scala/ch-2.scala | 11 |
7 files changed, 79 insertions, 0 deletions
diff --git a/challenge-171/laurent-rosenfeld/d/ch-2.d b/challenge-171/laurent-rosenfeld/d/ch-2.d new file mode 100644 index 0000000000..736b77ac0c --- /dev/null +++ b/challenge-171/laurent-rosenfeld/d/ch-2.d @@ -0,0 +1,13 @@ +import std.stdio; + +T delegate(S) compose(T, U, S)(in T delegate(U) f, + in U delegate(S) g) { + return s => f(g(s)); +} + +void main() { + auto h = compose((int x) => x / 2 + 1, (int x) => x * 2); + for (int i = 1; i <= 6; i++) { + writeln(i); + } +} diff --git a/challenge-171/laurent-rosenfeld/julia/ch-2.jl b/challenge-171/laurent-rosenfeld/julia/ch-2.jl new file mode 100644 index 0000000000..0256eb8775 --- /dev/null +++ b/challenge-171/laurent-rosenfeld/julia/ch-2.jl @@ -0,0 +1,13 @@ +function f(p) + return p / 2 + 1 +end +function g(p) + return p * 2 +end +function compose(h1, h2) + return h1 ∘ h2; +end +h = compose(f, g) +for i in 1:5 + println(h(i), " ", f(g(i))) +end diff --git a/challenge-171/laurent-rosenfeld/kotlin/ch-2.kt b/challenge-171/laurent-rosenfeld/kotlin/ch-2.kt new file mode 100644 index 0000000000..e16161e7e6 --- /dev/null +++ b/challenge-171/laurent-rosenfeld/kotlin/ch-2.kt @@ -0,0 +1,11 @@ +fun f(x: Int): Int = x/2 + 1 + +fun g(x: Int): Int = x * 2 + +fun compose(f: (Int) -> Int, g: (Int) -> Int): (Int) -> Int = { f(g(it)) } + +fun main() { + for (i in 2..6) { + println(compose(::f, ::g)(i)) + } +} diff --git a/challenge-171/laurent-rosenfeld/nim/ch-2.nim b/challenge-171/laurent-rosenfeld/nim/ch-2.nim new file mode 100644 index 0000000000..20cd66664a --- /dev/null +++ b/challenge-171/laurent-rosenfeld/nim/ch-2.nim @@ -0,0 +1,10 @@ +import sugar + +proc compose[A,B,C](h1: A -> B, h2: B -> C): A -> C = (x: A) => h1(h2(x)) + +proc f(x: int): int = int(x / 2) + 1 +proc g(x: int): int = x * 2 + +var h = compose(f, g) +for i in 1..6: + echo (h(i), f(g(i))) diff --git a/challenge-171/laurent-rosenfeld/python/ch-2.py b/challenge-171/laurent-rosenfeld/python/ch-2.py new file mode 100644 index 0000000000..01cb4943d5 --- /dev/null +++ b/challenge-171/laurent-rosenfeld/python/ch-2.py @@ -0,0 +1,11 @@ +def compose(f1, f2): + return lambda x: f1(f2(x)) +def f(p): + return p / 2 + 1 +def g(p): + return p * 2 + +h = compose(f, g) + +for i in range(1, 6): + print (f(g(i)), " ", h(i)) diff --git a/challenge-171/laurent-rosenfeld/ruby/ch-2.rb b/challenge-171/laurent-rosenfeld/ruby/ch-2.rb new file mode 100644 index 0000000000..167cf47cdd --- /dev/null +++ b/challenge-171/laurent-rosenfeld/ruby/ch-2.rb @@ -0,0 +1,10 @@ +def compose(h1, h2) + lambda {|x| h1.call(h2.call(x))} +end +f = lambda { |x| return x / 2 + 1 } +g = lambda { |x| return x * 2 } + +h = compose(f, g) +for i in 1.upto(6) + print ("#{h[i]} #{f.call(g.call(i))}\n") +end diff --git a/challenge-171/laurent-rosenfeld/scala/ch-2.scala b/challenge-171/laurent-rosenfeld/scala/ch-2.scala new file mode 100644 index 0000000000..358b93ee4f --- /dev/null +++ b/challenge-171/laurent-rosenfeld/scala/ch-2.scala @@ -0,0 +1,11 @@ +object first_class extends App { + def compose[A](h1: A => A, h2: A => A) = { x: A => h1(h2(x)) } + + def f(x: Int) = x / 2 + 1 + def g(x: Int) = x * 2 + + val h = compose(f, g) + for (i <- 1 to 6) { + println(s"${h(i)} ${f(g(i))}") + } +} |
