Skip to content

Instantly share code, notes, and snippets.

@aherve
Last active November 24, 2016 13:42
Show Gist options
  • Select an option

  • Save aherve/c4c6facac35a5c1f2318455e1ca5ec51 to your computer and use it in GitHub Desktop.

Select an option

Save aherve/c4c6facac35a5c1f2318455e1ca5ec51 to your computer and use it in GitHub Desktop.
derivative = -> f { # we take a function as argument
-> x { # the function takes a real x as argument
( f.(x+1e-3) - f.(x-1e-3) ) / ( 2e-3 ) # apply the scheme
}
}
# Let's try:
derivative_of_square = derivative.(square) => #<Proc:0x00000001b7c270@(irb):24 (lambda)> . Yay, a new function !
#Can we use it ?
square.(3) # => 9
derivative_of_square.(3) # => 5.999999999999339 ~ 2*3. Cool !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment