Browse Source

warumvogel

master
Norwin 1 year 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 @@
# ordinary, name based recursion. booooring!
fac = function (n) {
if (n < 2) 1
else n * fac(n - 1)
}

fac(8)

+ 16
- 0
01drossel.R View File

@@ -0,0 +1,16 @@
# Drossel: fn => fn(fn)
# usage: drossel(function(self, arg1, arg2))
# it's basically just a factory hiding the function name
# in a closure. easy!
drossel <- function (func) function (...) func(func, ...)

# example: factorial
# TODO: play with lazy evaluation?
fac_ <- function (self, n) {
if (n < 2) 1
else n * self(self, n - 1)
}

fac <- drossel(fac_)
fac(8)

+ 22
- 0
02warumvogel.R View File

@@ -0,0 +1,22 @@
# y-combinator.. i don't even
warumvogel <- function (f) {
selfinvoke <- function (x) x(x)
almostdrossel <- function (m) function (...) f(m(m), ...)
selfinvoke(almostdrossel)
}

# more compact
warumvogel <- function (f) {
(function (x) x(x))(
function (m) function (...) f(m(m), ...)
)
}

# todo: even more compact using S & K combinators?

fac <- warumvogel(function(self, n) {
if (n < 2) 1
else n * self(n - 1)
})

fac(8)

+ 13
- 7
README.md View File

@@ -3,18 +3,24 @@

Results of my exercises in gaining understanding of combinators, applied to R.

Inspired by the very approachable article [[1]] and it's followup [[2]],
implementations of the M- and Y-combinator in idiomatic R are shown (that is --
if there is such a thing as *idiomatic* R. lets call it functional R).
Inspired by the very approachable article [[1]] and its followup [[2]],
implementations of the M- and Y-combinator in idiomatic R are shown (that is, if
there is such a thing as *idiomatic* R; lets call it functional R).

If you want to use combinators in R, i would recommend looking at proper packages such as [[3]].
I did not look at their implementation beforehand (it's supposed to be a challenge after all),
so these implementations likely are better.
I did not find any R packages providing a combinator toolset, the closest is
this parser toolkit [[3]]. At some point I might extend this into a proper
package for any needs of functional madness.

[1]: http://raganwald.com/2018/08/30/to-grok-a-mockingbird.html
[2]: http://raganwald.com/2018/09/10/why-y.html
[3]: TODO
[3]: http://cran.mtu.edu/web/packages/Ramble/index.html

### Benchmarks
coming sooon
<!-- http://adv-r.had.co.nz/Profiling.html -->
<!-- Average timings of 10 runs on an X280 with an i7-8550U: -->

---

license: unlicense


+ 0
- 17
drossel.R View File

@@ -1,17 +0,0 @@
# Drossel: fn => fn(fn)
# usage: drossel(function(self, arg1, arg2))
drossel <- function (func) function (...) func(func, ...)

# example: x to the power of n
exp_n_ <- function (self, x, n) {
if (n == 0)
1
else if (n %% 2 == 1)
x * self(self, x * x, floor(n / 2))
else
self(self, x * x, n / 2)
}

exp_n <- drossel(exp_n_)
exp_n(2, 3)

Loading…
Cancel
Save