Skip to content

Commit

Permalink
Use new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Jan 7, 2025
1 parent 0ab48a3 commit ef3305c
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 365 deletions.
6 changes: 3 additions & 3 deletions lib/dry/monads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module Dry
module Monads
# @api private
def self.loader
@loader ||= Zeitwerk::Loader.new.tap do |loader|
root = File.expand_path("..", __dir__)
@loader ||= ::Zeitwerk::Loader.new.tap do |loader|
root = ::File.expand_path("..", __dir__)
loader.tag = "dry-monads"
loader.inflector = Zeitwerk::GemInflector.new("#{root}/dry-monads.rb")
loader.inflector = ::Zeitwerk::GemInflector.new("#{root}/dry-monads.rb")
loader.push_dir(root)
loader.ignore(
"#{root}/dry-monads.rb",
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/monads/conversion_stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Dry
module Monads
module ConversionStubs
def self.[](*method_names)
Module.new do
::Module.new do
method_names.each do |name|
define_method(name) do |*|
Methods.public_send(name)
Expand Down
29 changes: 12 additions & 17 deletions lib/dry/monads/lazy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def new(promise = nil, &)
# Forces the compution and returns its value.
#
# @return [Object]
def value!
@promise.execute.value!
end
def value! = @promise.execute.value!
alias_method :force!, :value!

# Forces the computation. Note that if the computation
Expand All @@ -42,23 +40,22 @@ def force
end

# @return [Boolean]
def evaluated?
@promise.complete?
end
def evaluated? = @promise.complete?
deprecate :complete?, :evaluated?

undef_method :wait

# @return [String]
def to_s
state = case promise.state
when :fulfilled
value!.inspect
when :rejected
"!#{promise.reason.inspect}"
else
"?"
end
state =
case promise.state
when :fulfilled
value!.inspect
when :rejected
"!#{promise.reason.inspect}"
else
"?"
end

"Lazy(#{state})"
end
Expand All @@ -79,9 +76,7 @@ module Constructors
#
# @param block [Proc]
# @return [Lazy]
def Lazy(&)
Lazy.new(&)
end
def Lazy(&) = Lazy.new(&)
end

include Constructors
Expand Down
84 changes: 21 additions & 63 deletions lib/dry/monads/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ class << self
#
# @param values [Array<Object>] List elements
# @return [List]
def [](*values)
new(values)
end
def [](*values) = new(values)

# Coerces a value to a list. `nil` will be coerced to an empty list.
#
Expand Down Expand Up @@ -144,9 +142,7 @@ def map(&block)
#
# @param other [List] Other list
# @return [List]
def +(other)
List.new(to_ary + other.to_ary)
end
def +(other) = List.new(to_ary + other.to_ary)

# Returns a string representation of the list.
#
Expand All @@ -167,24 +163,18 @@ def inspect
# Returns the first element.
#
# @return [Object]
def first
value.first
end
def first = value.first

# Returns the last element.
#
# @return [Object]
def last
value.last
end
def last = value.last

# Folds the list from the left.
#
# @param initial [Object] Initial value
# @return [Object]
def fold_left(initial, &)
value.reduce(initial, &)
end
def fold_left(initial, &) = value.reduce(initial, &)
alias_method :foldl, :fold_left
alias_method :reduce, :fold_left

Expand All @@ -200,52 +190,38 @@ def fold_right(initial)
# Whether the list is empty.
#
# @return [TrueClass, FalseClass]
def empty?
value.empty?
end
def empty? = value.empty?

# Sorts the list.
#
# @return [List]
def sort
coerce(value.sort)
end
def sort = coerce(value.sort)

# Filters elements with a block
#
# @return [List]
def filter(&)
coerce(value.select(&))
end
def filter(&) = coerce(value.select(&))
alias_method :select, :filter

# List size.
#
# @return [Integer]
def size
value.size
end
def size = value.size

# Reverses the list.
#
# @return [List]
def reverse
coerce(value.reverse)
end
def reverse = coerce(value.reverse)

# Returns the first element wrapped with a `Maybe`.
#
# @return [Maybe<Object>]
def head
Monads::Maybe.coerce(value.first)
end
def head = Monads::Maybe.coerce(value.first)

# Returns list's tail.
#
# @return [List]
def tail
coerce(value.drop(1))
end
def tail = coerce(value.drop(1))

# Turns the list into a typed one.
# Type is required for some operations like .traverse.
Expand All @@ -272,9 +248,7 @@ def typed(type = nil)
# Whether the list is types
#
# @return [Boolean]
def typed?
!type.nil?
end
def typed? = !type.nil?

# Traverses the list with a block (or without it).
# This methods "flips" List structure with the given monad (obtained from the type).
Expand Down Expand Up @@ -313,16 +287,12 @@ def apply(list = Undefined, &)
# Returns the List monad.
#
# @return [Monad]
def monad
List
end
def monad = List

# Returns self.
#
# @return [List]
def to_monad
self
end
def to_monad = self

# Iterates over the list and collects Some values.
#
Expand Down Expand Up @@ -367,15 +337,11 @@ def collect
# end
#
# @api private
def deconstruct
value
end
def deconstruct = value

private

def coerce(other)
self.class.coerce(other)
end
def coerce(other) = self.class.coerce(other)

# Empty list
EMPTY = List.new([].freeze).freeze
Expand All @@ -388,17 +354,11 @@ class << self

attr_reader :type

def initialize(type)
@type = type
end
def initialize(type) = @type = type

def [](*args)
List.new(args, type)
end
def [](*args) = List.new(args, type)

def coerce(value)
List.coerce(value, type)
end
def coerce(value) = List.coerce(value, type)

def pure(val = Undefined, &block)
value = Undefined.default(val, block)
Expand Down Expand Up @@ -433,9 +393,7 @@ module Mixin

# List constructor.
# @return [List]
def List(value)
List.coerce(value)
end
def List(value) = List.coerce(value)
end
end

Expand Down
Loading

0 comments on commit ef3305c

Please sign in to comment.