Git Product home page Git Product logo

los's People

Contributors

oubiwann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

longde123

los's Issues

Discussion on polymorphism idioms in LFE

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:

Add implementation for Clojure-like multi-methods

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:

Create defclass macro

This should be based on the example from Chapter 13, Section 4 of Peter Norvig's PAIP.

Copy OOP Examples from LFE to LOS

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.