Skip to content

Commit

Permalink
Pass block as arg, yield pairs instead of map
Browse files Browse the repository at this point in the history
  • Loading branch information
mwpastore committed Sep 16, 2016
1 parent 10ba4b6 commit b647500
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ IG = Object.ioughta_hash(->(i) { 1 << i }, %i[
shellfish
]).freeze

IG[:strawberries] # => 8
IG[:shellfish] # => 16
```

## Installation
Expand Down Expand Up @@ -86,6 +86,8 @@ include Ioughta

Object.ioughta_const(:FOO, :BAR, :QUX)

FOO # => 0
BAR # => 1
QUX # => 2
```

Expand All @@ -94,6 +96,8 @@ To skip value(s) in the sequence, use the `:_` symbol:
```ruby
Object.ioughta_const(:_, :FOO, :BAR, :_, :QUX)

FOO # => 1
BAR # => 2
QUX # => 4
```

Expand Down Expand Up @@ -123,7 +127,7 @@ Object.ioughta_const ->(i) { 1 << (10 * i) }, %i[_ KB MB GB TB PB EB ZB YB]
Or even pass a block, instead of a lambda:

```ruby
BYTES = Object.ioughta_hash(%i[_ KB MB GB TB PB EB ZB YB]) { |i| 1 << (10 * i) }.freeze
BYTES = Object.ioughta_hash(%i[_ KB MB GB TB PB EB ZB YB]) { |i| 10 ** (i * 3) }.freeze
```

The only major feature missing from the Go implementation is the ability to
Expand Down
26 changes: 12 additions & 14 deletions lib/ioughta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ module Ioughta
def self.included(base)
class << base
def ioughta_const(*data, &block)
each_resolved_pair(pair(data, &block)) do |nom, val|
each_resolved_pair(data, block) do |nom, val|
const_set(nom, val)
end
end

alias_method :iota_const, :ioughta_const

def ioughta_hash(*data, &block)
each_resolved_pair(pair(data, &block)).to_h
each_resolved_pair(data, block).to_h
end

alias_method :iota_hash, :ioughta_hash
Expand All @@ -23,24 +23,22 @@ def ioughta_hash(*data, &block)
DEFAULT_LAMBDA = proc(&:itself)
SKIP_SYMBOL = :_

def pair(data, &block)
data = data.flatten(1)
def each_pair_with_index(data, block = nil)
return enum_for(__method__, data, block) unless block_given?

data = data.to_a.flatten(1)
lam = (data.shift if data[0].respond_to?(:call)) || block || DEFAULT_LAMBDA

data.map.with_index do |nom, i, j = i.succ|
[nom, data[j].respond_to?(:call) ? lam = data.slice!(j) : lam]
data.each_with_index do |nom, i, j = i.succ|
yield nom, data[j].respond_to?(:call) ? lam = data.slice!(j) : lam, i
end
end

def each_resolved_pair(data)
return enum_for(__method__, data) do
data.count { |nom, | nom != SKIP_SYMBOL }
end unless block_given?
def each_resolved_pair(*args)
return enum_for(__method__, *args) unless block_given?

data.each_with_index do |(nom, lam), iota|
val = lam[*[iota, nom].take(lam.arity.abs)]
next if nom == SKIP_SYMBOL
yield nom, val
each_pair_with_index(*args) do |nom, lam, iota|
yield nom, lam[*[iota, nom].take(lam.arity.abs)] unless nom == SKIP_SYMBOL
end
end
end
Expand Down

0 comments on commit b647500

Please sign in to comment.