lfex / los Goto Github PK
View Code? Open in Web Editor NEWThis project forked from oubiwann/los
LFE Object System
This project forked from oubiwann/los
LFE Object System
This is from section 6 of the same chapter as in issue #3.
There was a blog post written about this topic here:
Code for exploring polymorphism continues to grow/adapt here:
This issue was opened in particular due to a tantalizing comment made by @rvirding on the LFE mail list -- he may have some nice ideas for better LFE idioms:
Usage should be something like this:
> (include-lib "los/include/multi-methods.lfe")
> (defmulti area 'type)
> (defmethod area 'rectangle
((`(#(length ,l) #(width ,w)))
(* l w)))
> (area '(#(type triangle) #(base 2) #(height 4)))
4.0
A non-macro version of this could be done in the following manner:
(defmodule polymorph
(export all))
(defun area
((`(,type . ,rest))
(dispatch 'area type rest)))
(defun dispatch
((fname `#(type ,type) args)
(call (MODULE) (list_to_atom (++ (atom_to_list fname)
"-"
(atom_to_list type))) args)))
(defun area-triangle
((`(#(base ,b) #(height ,h)))
(* b h (/ 1 2))))
(defun area-rectangle
((`(#(length ,l) #(width ,w)))
(* l w)))
These could then be used in the following manner:
(c "examples/no-macros/polymorph.lfe")
#(module polymorph)
(slurp "examples/no-macros/polymorph.lfe")
#(module polymorph)
> (area '(#(type triangle) #(base 2) #(height 4)))
4.0
> (area '(#(type rectangle) #(length 2) #(width 4)))
8
Additional implementations could be added with ease:
(defun area-square
((`(#(side ,s)))
(* s s)))
(defun area-circle
((`(#(radius ,r)))
(* (math:pi) r r)))
Then use them:
> (area '(#(type square) #(side 2)))
4
> (area '(#(type circle) #(radius 2)))
12.566370614359172
See the following:
This should be based on the example from Chapter 13, Section 4 of Peter Norvig's PAIP.
There should be 3 files in lfe/examples that we should move here.
As progress is made on LOS, those examples will be updated to utilize LOS and its growing features.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.