Skip to content

Instantly share code, notes, and snippets.

@kenwebb
Last active July 13, 2017 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenwebb/9c4ac75b1c43b0dd679aaac97c4732a7 to your computer and use it in GitHub Desktop.
Save kenwebb/9c4ac75b1c43b0dd679aaac97c4732a7 to your computer and use it in GitHub Desktop.
Lambda
<?xml version="1.0" encoding="UTF-8"?>
<!--Xholon Workbook http://www.primordion.com/Xholon/gwt/ MIT License, Copyright (C) Ken Webb, Thu Jul 13 2017 09:26:01 GMT-0400 (EDT)-->
<XholonWorkbook>
<Notes><![CDATA[
Xholon
------
Title: Lambda
Description:
Url: http://www.primordion.com/Xholon/gwt/
InternalName: 9c4ac75b1c43b0dd679aaac97c4732a7
Keywords:
My Notes
--------
July 12, 2017
In this workbook, I explore Lambda calculus and functions.
I will focus on JavaScript anonymous functions.
Here's a lambda that works with http://127.0.0.1:8888/XholonAQLonly.html?app=AQL+-+Web+Interface+-+AQLonly&src=lstr&gui=clsc
<XingCatTheoryInstancebehavior implName="org.primordion.xholon.base.Behavior_gwtjs">
var me;
const findValue = str => {
var arr = str.split(".");
var roleName = arr[0]; // "Bob"
var valName = arr[1]; // "mother"
var node = me.xpath("./*[@roleName='" + roleName + "']");
if (node != null) {
var retValue = node[valName];
if (retValue == null) {
return str;
}
return retValue;
}
return str;
}
var beh = {
postConfigure: function() {
me = this.cnode.parent();
// Bob.mother
var mother = findValue("Bob.mother");
$wnd.console.log(mother.toString());
this.cnode.remove();
}
}
</XingCatTheoryInstancebehavior>
References
----------
(1) https://en.wikipedia.org/wiki/Lambda_calculus
"Lambda calculus (also written as λ-calculus) is a formal system in mathematical logic for expressing computation based on function abstraction and application using variable binding and substitution. It is a universal model of computation that can be used to simulate any single-taped Turing machine and was first introduced by mathematician Alonzo Church in the 1930s as part of his research of the foundations of mathematics."
(2) https://stackoverflow.com/questions/16501/what-is-a-lambda-function
one answer starts with the following (and then provides examples in various languages including JavaScript and JavaScript ES6):
"Lambda comes from the Lambda Calculus and refers to anonymous functions in programming. Why is this cool? It allows you to write quick throw away functions without naming them. It also provides a nice way to write closures. With that power you can do things like this."
(3) David Flanagan, JavaScript 5th ed
Function literals p.33, 127
var f = function(x) {return x*x;} // define
var tensquared = (function(x) {return x*x;})(10); // define and invoke
(3) http://goodmath.blogspot.ca/2006/06/lamda-calculus-index.html
(4) http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html
) https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
Java 8 introduced lambdas
]]></Notes>
<_-.XholonClass>
<PhysicalSystem/>
<Lambda/>
<TestNode/>
</_-.XholonClass>
<xholonClassDetails>
</xholonClassDetails>
<PhysicalSystem>
<Lambda/>
<TestNode roleName="en"/>
<TestNode roleName="dva" age="42"/>
<TestNode roleName="trije"/>
<TestNode roleName="stirje"/>
<TestNode roleName="pet"/>
</PhysicalSystem>
<Lambdabehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
// JavaScript example from source [2]
var me, adder;
var beh = {
postConfigure: function() {
me = this.cnode.parent();
adder = function (x) {
return function (y) {
return x + y;
};
};
},
act: function() {
var add5 = adder(5);
me.println(add5(1) == 6);
}
}
]]></Lambdabehavior>
<Lambdabehavior implName="org.primordion.xholon.base.Behavior_gwtjs"><![CDATA[
// JavaScript ES6 example from source [2]
var me;
const adder = x => y => x + y;
const adder2 = (x, y) => x + y;
const findNode = rn => me.xpath("../TestNode[@roleName='" + rn + "']");
var beh = {
postConfigure: function() {
me = this.cnode.parent();
},
act: function() {
var add6 = adder(6);
me.println(add6(1) == 7);
me.println(adder2(20,22));
me.println(findNode("dva").age);
//findValue("age(dva)"); // TODO
}
}
]]></Lambdabehavior>
<SvgClient><Attribute_String roleName="svgUri"><![CDATA[data:image/svg+xml,
<svg width="100" height="50" xmlns="http://www.w3.org/2000/svg">
<g>
<title>Lambda</title>
<rect id="PhysicalSystem/Lambda" fill="#98FB98" height="50" width="50" x="25" y="0"/>
<g>
<title>Lambda</title>
<rect id="PhysicalSystem/Lambda" fill="#6AB06A" height="50" width="10" x="80" y="0"/>
</g>
</g>
</svg>
]]></Attribute_String><Attribute_String roleName="setup">${MODELNAME_DEFAULT},${SVGURI_DEFAULT}</Attribute_String></SvgClient>
</XholonWorkbook>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment