Skip to content

Instantly share code, notes, and snippets.

@ympbyc
Last active February 16, 2019 12:44
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 ympbyc/4996968 to your computer and use it in GitHub Desktop.
Save ympbyc/4996968 to your computer and use it in GitHub Desktop.
Example usecases of JS-CLOS https://github.com/ympbyc/js-clos
#||||||||||||||||||||||||||||||||||||||||
#||||| example of multiple dispatch |||||
#||||||||||||||||||||||||||||||||||||||||
floor = define_class []
carpet = define_class []
ball = define_class []
glass = define_class []
bump = define_generic()
define_method bump, [ball, floor], -> 'bounce'
define_method bump, [glass, floor], -> 'crash'
define_method bump, [undefined, carpet], -> 'silence'
bump ball, floor #bounce
bump (make ball), (make floor) #bounce
bump glass, floor #crach
bump ball, carpet #silence
bump glass, carpet #silence
#|||||||||||||||||||||||||||||||||||
#||||| example of multimethods |||||
#|||||||||||||||||||||||||||||||||||
#fact
fact = define_generic()
define_method fact, [0], -> 1
define_method fact, ["number"], (n) ->
n * fact (n - 1)
fact 5
#=> 120
# import
c = require 'clos'
this[name] = fn for name, fn of c
#|||||||||||||||||||||||||||||||||||||||||||
#||||| example of multiple inheritance |||||
#|||||||||||||||||||||||||||||||||||||||||||
# domain
animal = define_class [], (x) ->
slot_exists x, 'voice', 'string'
cat = define_class [animal]
osx = define_class [], (x) ->
slot_exists x, 'hostname', 'string'
lion = define_class [cat, osx]
# definitions
say = define_generic()
define_method say, [animal], (a) -> a.voice
define_method say, [cat], (c) -> c.voice + ' meow'
hostname = define_generic()
define_method hostname, [osx], (o) -> o.hostname
# main
alice = make lion,
voice: 'roar'
hostname: 'example.com'
say alice
#=> 'roar meow'
hostname alice
#=> 'example.com'
@5alamander
Copy link

5alamander commented May 9, 2016

Is this a bug?

  var fib = define_generic(true); 
  define_method(fib, [0], () => {return 0;});
  define_method(fib, [1], () => {return 1;});
  define_method(fib, ["number"], (n) => {
    return fib(n-1) + fib(n-2);
  });
  define_method(fib, [undefined], () => {return "default";});

  /// .....
  console.log(fib(17));
  console.log(fib({}));
  console.log(fib("number")); // ... error

@ympbyc
Copy link
Author

ympbyc commented Aug 17, 2016

Sorry for a late reply. I haven't been active on github lately.
It seems to be a bug indeed. Somewhere around these lines: https://github.com/ympbyc/js-clos/blob/master/clos.js#L117
I'll look into it when I have time. Thanks

@neverblued
Copy link

Hello, and sorry too for abandoning js-clos. Lately I came with a minimalistic lisp-on-JS implementation which though already has macros and dynamic scope. Just wanted to share the link now. https://github.com/neverblued/aynil

@ympbyc
Copy link
Author

ympbyc commented Feb 16, 2019

Thank you @neverblued That's nice.
Glad to hear from the original author!
JSCL seems exciting too.

@ympbyc
Copy link
Author

ympbyc commented Feb 16, 2019

Dicided this isn't a bug and expected behaviour. Fixing it will involve introducing something like CL's eql-specializer, which isn't a bad idea to pursue but because it's a sketch, if it works for 99% I say it's a pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment