Skip to content

Instantly share code, notes, and snippets.

@americodls
Created May 19, 2017 12:33
Show Gist options
  • Save americodls/20981b2864d166eee8d231904303f24b to your computer and use it in GitHub Desktop.
Save americodls/20981b2864d166eee8d231904303f24b to your computer and use it in GitHub Desktop.
How to languages implement method reference

Javascript

// Method Reference -> Math.sqrt
// Example

var roots = [1, 4, 9].map(Math.sqrt);

Java

import java.util.stream.DoubleStream;
// Method Reference -> Math::sqrt
// Example

double[] roots = DoubleStream.of(1, 4, 9).map(Math::sqrt).toArray();

Python

# Method Reference -> math.sqrt
# Example

import math
roots =  map(math.sqrt, [1, 4, 9])

Clojure

;; Method Reference -> #(Math/sqrt %)
;; Example

(map #(Math/sqrt %) [1, 4, 9])

Ruby

# Method Reference -> Math.method(:sqrt)
# Example
roots = [1, 4, 9].map &Math.method(:sqrt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment