JSFiddle - React, Tailwind, and code Playground

by jrab227

JavaScript

var fmap = function(f, p){
    return p.map(f)
}

//A list
var a = [1, 2, 3, 4]

//A tree
var t1 = new tree(2, null, null)
var t2 = new tree(3, null, null)
var t3 = new tree(1, t1, t2)
var t4 = new tree(5, null, null)
var t = new tree(10, t3, t4)


//General calling methods on lists and trees
fmap(function(n) { return n }, a)
fmap(function(n){ return n+1}, t)


//Tree definition
function tree(n, tree1, tree2){
    this.node= n
    this.left = tree1 
    this.right = tree2
    this.map = function(f){
        var self = this
        self.node = f(n)
        if (self.left != null && self.right != null){
            self.left = fmap(f, self.left), 
            self.right = fmap(f, self.right)
        }
        return self
    }
}