
Yesterday we started a Hyperlambda course. The objective was to teach all the basics of Aista and Hyperlambda. Today we finished with some more Hyperlambda, diving into the code generator, how it works, and how Hyperlambda is able to dynamically create and modify your code. Below are all sessions from today.
- Session 1, Hyperlambda syntax, modifying your tree, and dynamically modifying code
- Session 2, more dynamic modifying code concepts, conditional branching of code, if, else-if, else and looping
- Session 3, expressions, more modifications of our tree, and dynamically created slots
Day 2 of our Hyperlambda course we're diving much deeper into the language itself, and studying control structures such as conditional branching, loops, and such. The objective is to teach a junior developer everything required to learn to use Aista as a software development platform, and use Hyperlambda as a programming language.
Below are the code snippets I am using in the above videos. The first snippet demonstrates a simple for each loop.
/*
* An example of a [for-each] that iterates through a collection
* of nodes, and manipulates the values of each node it is iterating.
*/
.data
foo1:bar1
foo2:bar2
foo3:bar3
for-each:x:@.data/*
set-value:x:@.dp/#
.:For each was here!
The second snippet illustrates how to add nodes during the for each loop's iteration.
/*
* An example of a [for-each] that iterates through a collection
* of nodes, and adds a new node to each node it is iterating over.
*/
.data
foo1:bar1
foo2:bar2
foo3:bar3
for-each:x:@.data/*
add:x:@.dp/#
.
new-node-was-here
The third snippet illustrates how to dynamically change a lambda object.
/*
* An example of dynamically creating a lambda object.
*/
.data
foo1:bar1
foo2:bar2
foo3:bar3
remove-nodes:x:@.data/*
add:x:@.data
.
log.info:This was dynamically injected into our lambda object
add:x:@.data
.
http.get:"https://aista.com"
The fourth snippet shows how to dynamically inject code into a lambda object, for then to persist the snippet into our database. Notice, the database script can be found below the code snippet.
/*
* An example of dynamically creating a lambda object.
*/
.data
foo1:bar1
foo2:bar2
foo3:bar3
// Removes the existing nodes in our [.data] node.
remove-nodes:x:@.data/*
// Adds a [log.info] invocation into our lambda object.
add:x:@.data
.
log.info:This was dynamically injected into our lambda object
// Adds an invocation to [http.get] into our above lambda object.
add:x:@.data
.
http.get:"https://aista.com"
// Transforming our lambda object into Hyperlambda
lambda2hyper:x:@.data/*
// Storing our Hyperlambda into our database.
data.connect:code
data.create
table:snippets
values
snippet_content:x:@lambda2hyper
The comes the SQL required to create your database.
/*
* Automatically generated by Magic.
*/
CREATE TABLE snippets(
snippet_id integer not null primary key autoincrement,
snippet_content text
);
The next snippet shows how to load that code dynamically from our database, and execute it.
/*
* Dynamically load Hyperlambda from our database and execute it!
*/
data.connect:code
data.read
table:snippets
columns
snippet_content
where
and
snippet_id.eq:1
// Transforming the Hyperlambda to a lambda object
hyper2lambda:x:@data.read/*/*/snippet_content
// Executing the lambda object.
eval:x:@hyper2lambda
The next snippet shows how to dynamically edit all code files inside a folder of your choice.
/*
* Script that formats all Hyperlambda files recursively within
* the specified folder.
*/
.folder:/modules/tasks/
io.file.list-recursively:x:-
for-each:x:-/*
if
strings.ends-with:x:@.dp/#
.:.post.hl
.lambda
// Hyperlambda file is an HTTP POST endpoint.
io.file.load:x:@.dp/#
hyper2lambda:x:-
comments:true
insert-before:x:@hyper2lambda/*/data.connect
.
log.info:Thank you for providing me with valuable data!
lambda2hyper:x:@hyper2lambda/*
comments:true
io.file.save:x:@.dp/#
get-value:x:@lambda2hyper
else-if
strings.ends-with:x:@.dp/#
.:.delete.hl
.lambda
// Hyperlambda file is an HTTP DELETE endpoint.
io.file.load:x:@.dp/#
hyper2lambda:x:-
comments:true
insert-before:x:@hyper2lambda/*/data.connect
.
log.info:OHH NOO! We will miss you!!
lambda2hyper:x:@hyper2lambda/*
comments:true
io.file.save:x:@.dp/#
get-value:x:@lambda2hyper
The next snippet illustrates usage of the while loop.
/*
* Iterates for as long as there are chldren nodes of [.src],
* and adds the first node from [.src] into the [.dest] node, for
* then to delete the first node from [.srcv].
*/
.src
foo1:bar1
foo2:bar2
foo3:bar3
.dest
while
exists:x:@.src/*
.lambda
add:x:@.dest
get-nodes:x:@.src/0
remove-nodes:x:@.src/0
The next snippet shows how to iterate through a bunch of nodes and adding the values together creating an aggregated result.
// Data segments with a bunch of integers.
.data
value1:int:2
value2:int:7
value3:int:11
// Result node holding the result of our [math.add] invocations.
.result:int:0
// Iterating through all integers in our above data segment.
for-each:x:@.data/*
set-value:x:@.result
math.add:x:@.result
get-value:x:@.dp/#
The next snippet shows how to braid together nodes with the result set of a select SQL statement executing towards your database.
data.connect:magic
data.read
table:log_entries
columns
content
limit:5
add:x:@data.read/*/*
.
copyright:Aista, Ltd