Two really cool Active Events

I’ve created these Active Events previously in System42, but I have just now significantly improved them both, in another version of them, which I intend to release in the upcoming release of Micro. First of all, because I want Micro to stand on its own two feet and not be dependent upon System42 in anyways. Secondly, because I realised I could significantly improve my logic in both of them, but didn’t want to break backwards compatibility in System42.

One really cool trait of Hyperlambda, is at what ease it allows you to treat lambda objects. Lambda objects are function objects, which you can invoke, and evaluate as you see fit. Hence, I often find myself wanting to treat files and snippets of text as lambda objects, and automatically evaluate them, in addition to passing in arguments while doing so. So creating intelligent “evaluate file” events, is basically a given.

Below are two kick-as cool events, that allows you to do just that. The first event will evaluate any given Hyperlambda file, allowing the caller to pass in arguments, and return lambda objects and values from the evaluation of these files.

The second event allows you to recursively evaluate an entire folder, such that it will evaluate all Hyperlambda files within a specific folder, for then to invoke itself for each folder within that folder, etc. The second event happens to be really cool for running startup scripts and such, allowing you to initialise your apps and components, with a single line of code – Resulting in that every single Hyperlambda file within some folder hierarchy becomes evaluated.

Both of these events can be given arguments, and can return values to the caller, allowing you to treat multiple Hyperlambda files, and/or entire folder structures of Hyperlambda files, as “function objects”. But instead of me waving my hands, feel free to check them out for yourselves …

Evaluate one or more files

/*
 * Creates the Active Event that allows you to execute one or more 
 * Hyperlambda files. 
 *
 * The Hyperlambda file(s) you wish to execute is given as [_arg].
 * All other arguments will be passed into your execution as is.
 */
create-event:micro.evaluate.file

  /*
   * Used as "signal object" to separate arguments from the 
   * rest of the lambda.
   */
  .signal

  /*
   * Loading up all files given as [_arg] arguments.
   */
  load-file:x:/@.signal/--/<-/_arg?value

  /*
   * Adding all arguments given to evaluation of file into [eval].
   */
  add:x:/../*/eval
    src:x:/@.signal/--(!/_arg)/<-

  /*
   * Evaluating [load-file]'s children, with the arguments 
   * applied in the above [add] invocation.
   */
  eval:x:/@load-file/*

  /*
   * Returning the results of the above evaluation to caller.
   */
  insert-after:x:/@.signal
    src:x:/@eval/*
  set:x:/..?value
    src:x:/@eval?value

Evaluate one or more folders recursively

/*
 * Evaluates all Hyperlambda files within the specified folder(s) 
 * recursively.
 *
 * The Hyperlambda folder(s) you wish to execute is given as [_arg].
 * All other arguments will be passed into your execution as is.
 */
create-event:micro.evaluate.folder

  /*
   * Used as "signal object" to separate arguments from the rest 
   * of the lambda.
   */
  .signal

  /*
   * Listing all Hyperlambda files in directories we're given.
   */
  list-files:x:/../*/_arg?value
    filter:.hl

  /*
   * Evaluating all Hyperlambda files from that folder, making 
   * sure we pass in arguments given.
   */
  add:x:/+
    src:x:/@.signal/--(!/_arg)/<-
  micro.evaluate.file:x:/@list-files/*?name

  /*
   * Returning the results of the above evaluation to caller.
   */
  insert-after:x:/@.signal
    src:x:/@micro.evaluate.file/*
  set:x:/..?value
    src:x:/@eval?value

  /*
   * Listing all folders recursively, within the folder(s) we've 
   * just evaluated.
   */
  list-folders:x:/../*/_arg?value

  /*
   * If the above [list-folder] gave us any result(s), we recursively 
   * invoke self, making sure we pass in the same arguments we were 
   * given.
   */
  if:x:/-/*
    add:x:/+
      src:x:/@.signal/--(!/_arg)/<-
    micro.evaluate.folder:x:/@list-folders/*?name

Not bad for roughly 30 lines of actual code. The above two events would condense your syntax, and give you features, no other programming language on the planet I am currently aware of actually have. And these features happens to be really, really cool! Usage could be for instance.

/*
 * Evaluates all Hyperlambda files recursively within your "/x/y/" folder.
 */
micro.evaluate.folder:/x/y/
  argument-1:Foo

The above will recursively traverse your “/x/y/” folder, and evaluate all Hyperlambda files within this folder, and its folders, etc – Passing in [argument-1] to every single file evaluated this way. For the record, both of these events will be released as publicly supported Active Events in the upcoming release of Micro.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s