Class: Keisan::Functions::Sample

Inherits:
ProcFunction show all
Defined in:
lib/keisan/functions/sample.rb

Instance Attribute Summary

Attributes inherited from ProcFunction

#function_proc

Attributes inherited from Keisan::Function

#arity, #name

Instance Method Summary collapse

Methods inherited from ProcFunction

#evaluate, #simplify, #value

Methods inherited from Keisan::Function

#differentiate, #evaluate, #simplify, #unbound_variables, #value

Constructor Details

#initializeSample

Returns a new instance of Sample.



4
5
6
7
# File 'lib/keisan/functions/sample.rb', line 4

def initialize
  @name = "sample"
  @arity = ::Range.new(1, 2)
end

Instance Method Details

#call(context, *args) ⇒ Object

Single argument: list to sample element from Double argument: list and number of elements to sample



11
12
13
14
15
16
17
18
19
20
# File 'lib/keisan/functions/sample.rb', line 11

def call(context, *args)
  case args.size
  when 1
    args.first.sample(random: context.random)
  when 2
    args[0].sample(args[1], random: context.random)
  else
    raise Exceptions::InvalidFunctionError.new
  end
end