Browse Source

warumvogel

master
Norwin Roosen 10 months ago
parent
commit
899dfa7a58
5 changed files with 58 additions and 24 deletions
  1. 7
    0
      00boring.R
  2. 16
    0
      01drossel.R
  3. 22
    0
      02warumvogel.R
  4. 13
    7
      README.md
  5. 0
    17
      drossel.R

+ 7
- 0
00boring.R View File

@@ -0,0 +1,7 @@
1
+# ordinary, name based recursion. booooring!
2
+fac = function (n) {
3
+  if (n < 2) 1
4
+  else n * fac(n - 1)
5
+}
6
+
7
+fac(8)

+ 16
- 0
01drossel.R View File

@@ -0,0 +1,16 @@
1
+# Drossel: fn => fn(fn)
2
+# usage: drossel(function(self, arg1, arg2))
3
+# it's basically just a factory hiding the function name
4
+# in a closure. easy!
5
+drossel <- function (func) function (...)  func(func, ...)
6
+
7
+# example: factorial
8
+# TODO: play with lazy evaluation?
9
+fac_ <- function (self, n) {
10
+  if (n < 2) 1
11
+  else n * self(self, n - 1)
12
+}
13
+
14
+fac <- drossel(fac_)
15
+  
16
+fac(8)

+ 22
- 0
02warumvogel.R View File

@@ -0,0 +1,22 @@
1
+# y-combinator.. i don't even
2
+warumvogel <- function (f) {
3
+  selfinvoke <- function (x) x(x)
4
+  almostdrossel <- function (m) function (...) f(m(m), ...)
5
+  selfinvoke(almostdrossel)
6
+}
7
+
8
+# more compact
9
+warumvogel <- function (f) {
10
+  (function (x) x(x))(
11
+    function (m) function (...) f(m(m), ...)
12
+  )
13
+}
14
+
15
+# todo: even more compact using S & K combinators?
16
+
17
+fac <- warumvogel(function(self, n) {
18
+  if (n < 2) 1
19
+  else n * self(n - 1)
20
+})
21
+
22
+fac(8)

+ 13
- 7
README.md View File

@@ -3,18 +3,24 @@
3 3
 
4 4
 Results of my exercises in gaining understanding of combinators, applied to R.
5 5
 
6
-Inspired by the very approachable article [[1]] and it's followup [[2]],
7
-implementations of the M- and Y-combinator in idiomatic R are shown (that is --
8
-if there is such a thing as *idiomatic* R. lets call it functional R).
6
+Inspired by the very approachable article [[1]] and its followup [[2]],
7
+implementations of the M- and Y-combinator in idiomatic R are shown (that is, if
8
+there is such a thing as *idiomatic* R; lets call it functional R).
9 9
 
10
-If you want to use combinators in R, i would recommend looking at proper packages such as [[3]].
11
-I did not look at their implementation beforehand (it's supposed to be a challenge after all),
12
-so these implementations likely are better.
10
+I did not find any R packages providing a combinator toolset, the closest is
11
+this parser toolkit [[3]].   At some point I might extend this into a proper
12
+package for any needs of functional madness.
13 13
 
14 14
 [1]: http://raganwald.com/2018/08/30/to-grok-a-mockingbird.html
15 15
 [2]: http://raganwald.com/2018/09/10/why-y.html
16
-[3]: TODO
16
+[3]: http://cran.mtu.edu/web/packages/Ramble/index.html
17
+
18
+### Benchmarks
19
+coming sooon
20
+<!-- http://adv-r.had.co.nz/Profiling.html -->
21
+<!-- Average timings of 10 runs on an X280 with an i7-8550U: -->
17 22
 
18 23
 ---
19 24
 
20 25
 license: unlicense
26
+

+ 0
- 17
drossel.R View File

@@ -1,17 +0,0 @@
1
-# Drossel: fn => fn(fn)
2
-# usage: drossel(function(self, arg1, arg2))
3
-drossel <- function (func) function (...)  func(func, ...)
4
-
5
-# example: x to the power of n
6
-exp_n_ <- function (self, x, n) {
7
-  if (n == 0)
8
-    1
9
-  else if (n %% 2 == 1)
10
-    x * self(self, x * x, floor(n / 2))
11
-  else
12
-    self(self, x * x, n / 2)
13
-}
14
-
15
-exp_n <- drossel(exp_n_)
16
-  
17
-exp_n(2, 3)

Loading…
Cancel
Save