Class: Alda::InlineLisp

Inherits:
Event
  • Object
show all
Defined in:
lib/alda-rb/event.rb

Overview

An inline lisp event. An Alda::EventContainer containing an Alda::InlineLisp can be derived using event list sugar (see Alda::EventList#method_missing) or by using Alda::EventList#l.

Sometimes you need help from Alda::LispIdentifier.

It serves as attributes in alda codes.

Alda::Score.new do
  tempo! 108
  quant! 200
  piano_ c e g violin_ g2 e4
end

Here, tempo! 108 and quant! 200 are inline lisp events and serves for alda attributes.

It can participate in the sequence sugar if it is at the end of the sequence.

Alda::Score.new do
  piano_ c d e quant 200
  g o! c o? c2
end

You can operate a score by purely using inline lisp events. The following example only works in Alda 1 due to breaking changes in Alda 2 (alda-lang/alda#483, alda-lang/alda#484).

Alda::Score.new do
  part 'piano'
  key_sig [:d, :major]
  note pitch :d
  note pitch :e
  note pitch(:f), duration(note_length 2)
end

When using event list sugar to create inline lisp events, note that it is not previously defined as a variable. See Alda::SetVariable and Alda::GetVariable.

Alda::Score.new do
  piano_
  p barline.event.class # => Alda::InlineLisp
  barline__ c d e f
  p barline.event.class # => Alda::GetVariable
end

Whether it is an Alda::SetVariable, Alda::InlineLisp, or Alda::GetVariable is intelligently determined.

Alda::Score.new do
  piano_
  p((tempo 108).event.class)  # => Alda::InlineLisp
  p tempo { c d }.event.class # => Alda::SetVariable
  p tempo.event.class         # => Alda::GetVariable
  p((tempo 60).event.class)   # => Alda::InlineLisp
  p to_s
  # => "piano: (tempo 108) tempo = [c d]\n tempo (tempo 60)"
end

If you want, you can generate lisp codes using ruby.

Alda.generation = :v1
Alda::Score.new do
  println reduce _into_, {}, [{dog: 'food'}, {cat: 'chow'}]
end.save 'temp.clj'
`clojure temp.clj` # => "{:dog food, :cat chow}\n"

Instance Attribute Summary collapse

Attributes inherited from Event

#container, #parent

Instance Method Summary collapse

Methods inherited from Event

#detach_from_parent, #is_event_of?

Constructor Details

#initialize(head, *args) ⇒ InlineLisp

:call-seq:

new(head, *args) -> Alda::InlineLisp

Creates a new Alda::InlineLisp.

The underlines “_” in head will be converted to hyphens “-”.



431
432
433
434
435
# File 'lib/alda-rb/event.rb', line 431

def initialize head, *args
	super()
	@head = Alda::Utils.snake_to_slug head
	@args = args
end

Instance Attribute Details

#argsObject

The arguments passed to the lisp function.

Its elements can be any object that responds to to_alda_code and detach_from_parent.



422
423
424
# File 'lib/alda-rb/event.rb', line 422

def args
  @args
end

#headObject

The function name of the lisp function



415
416
417
# File 'lib/alda-rb/event.rb', line 415

def head
  @head
end

Instance Method Details

#==(other) ⇒ Object

:call-seq:

inline_lisp == other -> true or false

Overrides Alda::Event#==. Returns true if other is an Alda::InlineLisp and has the same #head and #args as inline_lisp (using ==).



460
461
462
# File 'lib/alda-rb/event.rb', line 460

def == other
	super || other.is_a?(Alda::InlineLisp) && @head == other.head && @args == other.args
end

#on_containedObject

See Alda::Event#on_contained.



448
449
450
451
# File 'lib/alda-rb/event.rb', line 448

def on_contained
	super
	@args.detach_from_parent
end

#to_alda_codeObject

:call-seq:

to_alda_code() -> String

Overrides Alda::Event#to_alda_code.



442
443
444
# File 'lib/alda-rb/event.rb', line 442

def to_alda_code
	"(#{head} #{args.map(&:to_alda_code).join ' '})"
end