Skip to content

Instantly share code, notes, and snippets.

@michaelahlers
Last active April 2, 2019 19:12
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 michaelahlers/84f815ec1d58372f12abecef50784b68 to your computer and use it in GitHub Desktop.
Save michaelahlers/84f815ec1d58372f12abecef50784b68 to your computer and use it in GitHub Desktop.
Pyramid Generator Challenge!

Scala should have a shot at this classic nerd fight.

Java vs. Python

Concise version:

(1 to 5).map("*"*).foreach(println)

Uses language features:

  1. Implicit conversion to wrap values with decorator classes. (These add the to and * functions to the 1 and "*" literals respectively.)
  2. Placeholder parameters allowing partial application of functions passed as arguments.

A desugared version of that line would be:

(1 to 5) map { i => "*" * i } foreach { s => println(s) }

Using language features:

  1. The first feature from the previous snippet.
  2. Function literals using the => syntax sugar, which expands to Function1[-T1, R].
@fetchak
Copy link

fetchak commented Apr 2, 2019

Ruby:

puts (1..5).map { |i| '*' * i }

@fetchak
Copy link

fetchak commented Apr 2, 2019

A JS/ES2015+ attempt:

[...Array(5).keys()].forEach(i => console.warn(''.padEnd(i+1, '*')))

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