juan_gandhi: (Default)
(disclaimer: this is educational material)

  // Definitions for Boolean Logic
  const True  = x => y => x
  const False = x => y => y
  const Bool = b => b ? True : False

  // Definitions for pair and projection
  const p1 = x => y => x // did you notice? It's True
  const p2 = x => y => y // did you notice? It's False
  const Pair = x => y => f => f(x)(y)

  // Option constructors
  const None = Pair(True)("None")
  const Some = x => Pair(False)(x)

  // List constructors and operations
  const Nil = Pair(True)("Nil")
  const isEmpty = x => x(p1)
  const Head = z => z(p2)(p1)
  const HeadOption = z => z(p1)(None)(Some(z(p2)(p1)))
  const Tail = z => z(p2)(p2)
 
  const Cons = h => t => Pair(False)(Pair(h)(t))
  const Map = z => f => (isEmpty(z) (() => Nil) (() => Cons(f(Head(z))) (Map(Tail(z))(f))))()

  // visualizer

  const show = text => xs => {
    var buf = ""
    Map(xs)(x => buf += (x + ":"))
    println(`${text} ${buf}Nil`)
  }

  const Filter  = z => p =>
     (isEmpty(z) 
       (() => Nil) 
       (() => (p(Head(z)) (Cons(Head(z))(Filter(Tail(z))(p))) (Filter(Tail(z))(p))))
      )() // have to do it lazily

  // samples
  const ab = Cons("a")(Cons("b")(Nil))

  show("Expecting Nil ->")(Nil)
  show("Expecting a:b:Nil ->")(ab)

  const list1 = Cons(1)(Cons(-2)(Cons(3)(Cons(-4)(Nil))))

  show("three numbers")(list1)
  const isPositive = x => Bool(x > 0)

  show("squares")(Map(list1)(x => x*x))
  show("positives")(Filter(list1)(isPositive)) 

juan_gandhi: (Default)
// Definitions for Boolean Logic
const True = x => y => x
const False = x => y => y
const Bool = b => b ? True : False

// Definitions for pair and projection
const p1 = x => y => x // did you notice? It's True
const p2 = x => y => y // did you notice? It's False
const Pair = x => y => f => f(x)(y)

// Option constructors and operations
const None = Pair(True)("None")
const Some = x => Pair(False)(x)
const isEmpty = x => x(p1)
const FlatMap = x => f => isEmpty(x) (None) ( f(x(p2)))
const Map = x => f => isEmpty(x) (None) (Some(f(x(p2))))
const Filter = x => p => isEmpty(x) (None) (p(x(p2)) (x) (None))

// visualizer
const show = text => xOpt => {
Map(xOpt)(x => println(text + x))
}

// samples
show("Expecting Some ")(Some(42))
show("Expecting None ")(None)

const isPositive = x => Bool(x > 0)
show("0 positive? ")(Filter(Some(0))(isPositive))
show("2 positive? ")(Filter(Some(2))(isPositive))

const dec = n => n > 10 ? Some(n-10) : None

show("expecting None from None: ")(FlatMap( None)(dec))
show("expecting None from 5: ") (FlatMap(Some(5) )(dec))
show("expecting 32 from 42: ") (FlatMap(Some(42))(dec))
juan_gandhi: (Default)

const option = x =>
typeof x === 'undefined' ? None : Some(x)

const None = {
map: f => None,
flatMap: f => None,
toString: () => 'None'
}

const Some = x => ({
flatMap: (f => f(x)),
map: (f => option(f(x))),
toString: () => `Some(${x})`
})

 
juan_gandhi: (Default)
const iterate = f => z => pair(z, () => iterate(f)(f(z)));

const integers = iterate(n => n+1)(0);
juan_gandhi: (Default)
here - beautiful images!
juan_gandhi: (Default)
Джавасприпт, Хаскель и Скала, и, говорят, OCaml, из-за своей компактности, канают в качестве скриптовых языков.

Питон в этом смысле тоже ничо, но там, во-первых, споткнуться можно много где, во-вторых, на уровне компиляции нам ничего не скажут. Это уже на продакшене ебанется. То-то квора, которая на питоне, деплоится по сто раз в час (а тесты потом гоняют, и кодревью тоже). 
juan_gandhi: (VP)
function* foo(){
  var index = 0;
  while (index <= 2) // when index reaches 3, 
                     // yield's done will be true 
                     // and its value will be undefined;
    yield index++;
}
var iterator = foo();
println(iterator.next().value); // { value: 0, done: false }
println(iterator.next().value); // { value: 1, done: false }
println(iterator.next().value); // { value: 2, done: false }
println(iterator.next().value); // { value: undefined, done: true }
juan_gandhi: (VP)
Y = function (F) {
 var run = function (x) { return F(function (y) { return (x(x))(y)})}
 return run(run)
};
juan_gandhi: (VP)
/*
S combinator: \x \y \z x z (y z)
*/
window.S = function (x) { return function(y) { return function(z) { return x(z)(y(z)) } } };

/*
I combinator: \x x
*/
window.I = function(x) { return x };

/*
K combinator: \x \y x
*/
window.K = function(x) { return function() { return x }}

/*
Y combinator
*/
window.Y = Y = S (K(S(I)(I))) (S(S(K(S))(K)) (K(S(I)(I))));
juan_gandhi: (VP)
Только что в библиотеку добавил
window.K = function(x){return function(){return x}}
juan_gandhi: (VP)


<script>


function environmentName()
{
	var env = 'prod';
	var envCaps = env.toUpperCase();

	//alert("prod" + "  " + envCaps);
	return envCaps;
}
</script>


(repeated twice on each page)
juan_gandhi: (VP)
window.fold = function(list, zero, op) {
    var r = zero;
    for (var i = 0; i < list.length; i++) {
        r = op(r, list[i])
    }
    return r;
};

window.flatten = function(listOfLists) { 
    return fold(listOfLists, [], function(s1, s2) { return s1.concat(s2)})
};


Basically, I do map/reduce.
juan_gandhi: (VP)

window.project = (list, field) => map(list, (x) => x[field]);


I know, a list is supposed to have a map methods. But I don't observe it yet.

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

October 2025

S M T W T F S
    1 23 4
5 678 9 1011
12 13 1415 161718
1920 2122 23 2425
26 2728293031 

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Oct. 28th, 2025 06:53 am
Powered by Dreamwidth Studios