Skip to main content
Code Golf

Return to Answer

22 (`->n{n.times{|i|p i+1}}`) -> 20(`->n{n.times{p _1+1}}`) characters
Source Link
->n{i=0;loop{p i+=1;i<n||break}} # 32 bytes
f=->n,i=1{i>n||p(i)&&f[n,i+1]} # 30 bytes
->n,i=0{p(i+=1)<n&&redo} # 24 bytes
->n{i=0;p i+=1while i<n} # 24 bytes
->n{i=0;eval"p i+=1;"*n} # 24 bytes
->n{n.times{|i|p i+1}} # 22 bytes # thanks to @benj2240
->n{n.times{p _1+1}} # 20 bytes # thanks to @AgentIvan
->n{i=0;loop{p i+=1;i<n||break}} # 32 bytes
f=->n,i=1{i>n||p(i)&&f[n,i+1]} # 30 bytes
->n,i=0{p(i+=1)<n&&redo} # 24 bytes
->n{i=0;p i+=1while i<n} # 24 bytes
->n{i=0;eval"p i+=1;"*n} # 24 bytes
->n{n.times{|i|p i+1}} # 22 bytes # thanks to @benj2240
->n{i=0;loop{p i+=1;i<n||break}} # 32 bytes
f=->n,i=1{i>n||p(i)&&f[n,i+1]} # 30 bytes
->n,i=0{p(i+=1)<n&&redo} # 24 bytes
->n{i=0;p i+=1while i<n} # 24 bytes
->n{i=0;eval"p i+=1;"*n} # 24 bytes
->n{n.times{|i|p i+1}} # 22 bytes # thanks to @benj2240
->n{n.times{p _1+1}} # 20 bytes # thanks to @AgentIvan
Commonmark migration
Source Link

#On looping

On looping

###while...end

while...end

###redo

redo

###recursion

recursion

###eval

eval

##Examples

Examples

###A lambda to print all numbers from 1 to a inclusive:

A lambda to print all numbers from 1 to a inclusive:

###Given a function g and a number n, find the first number strictly larger than n for which g[n] is truthy

Given a function g and a number n, find the first number strictly larger than n for which g[n] is truthy

#On looping

###while...end

###redo

###recursion

###eval

##Examples

###A lambda to print all numbers from 1 to a inclusive:

###Given a function g and a number n, find the first number strictly larger than n for which g[n] is truthy

On looping

while...end

redo

recursion

eval

Examples

A lambda to print all numbers from 1 to a inclusive:

Given a function g and a number n, find the first number strictly larger than n for which g[n] is truthy

shortened some examples
Source Link
Asone Tuhid
  • 2.4k
  • 9
  • 23
->g,n{(n+1..1/0.0).find{|i|g[i]}} # 33 bytes
->g,n{loop{g[n+=1]&&break};n} # 29 bytes
f=->g,n{n+=1;g[n]g[n+=1]?n:f[g,n]} # 2725 bytes
->g,n{1until g[n+=1];n} # 23 bytes
->g,n{(n+1..).find &g} # 22 bytes
->g,n{g[n+=1]?n:redo} # 21 bytes

The eval method is not viable in this case because there is neither a defined end-point nor an upper bound.

Update: with the new open ranges (n+1..Inf) can be written simply as (n+1..), also .find{|x|g[x]} is equivalent to .find &g where g is converted to a block.


TL;DR check out redo, it can very often shave off a couple of bytes

->g,n{(n+1..1/0.0).find{|i|g[i]}} # 33 bytes
->g,n{loop{g[n+=1]&&break};n} # 29 bytes
f=->g,n{n+=1;g[n]?n:f[g,n]} # 27 bytes
->g,n{1until g[n+=1];n} # 23 bytes
->g,n{g[n+=1]?n:redo} # 21 bytes

The eval method is not viable in this case because there is neither a defined end-point nor an upper bound.

->g,n{loop{g[n+=1]&&break};n} # 29 bytes
f=->g,n{g[n+=1]?n:f[g,n]} # 25 bytes
->g,n{1until g[n+=1];n} # 23 bytes
->g,n{(n+1..).find &g} # 22 bytes
->g,n{g[n+=1]?n:redo} # 21 bytes

The eval method is not viable in this case because there is neither a defined end-point nor an upper bound.

Update: with the new open ranges (n+1..Inf) can be written simply as (n+1..), also .find{|x|g[x]} is equivalent to .find &g where g is converted to a block.


TL;DR check out redo, it can very often shave off a couple of bytes

added 2 characters in body
Source Link
Asone Tuhid
  • 2.4k
  • 9
  • 23
Loading
golfed a few loops
Source Link
Asone Tuhid
  • 2.4k
  • 9
  • 23
Loading
added 1560 characters in body
Source Link
Asone Tuhid
  • 2.4k
  • 9
  • 23
Loading
added 179 characters in body
Source Link
Asone Tuhid
  • 2.4k
  • 9
  • 23
Loading
added 76 characters in body
Source Link
Asone Tuhid
  • 2.4k
  • 9
  • 23
Loading
Source Link
Asone Tuhid
  • 2.4k
  • 9
  • 23
Loading

AltStyle によって変換されたページ (->オリジナル) /