Skip to content
This repository has been archived by the owner on Apr 12, 2019. It is now read-only.

Commit

Permalink
Alphabetize the methods in List
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Arcieri committed Feb 12, 2011
1 parent 7257199 commit 584be60
Showing 1 changed file with 75 additions and 80 deletions.
155 changes: 75 additions & 80 deletions src/builtins/list.re
Expand Up @@ -28,6 +28,36 @@ class List
replace(index, value) replace(index, value)
end end
end end

# Takes a conditional block and returns true if all elements meet the
# condition, false otherwise
def all?(&block)
erl.lists.all(block, self)
end

# Takes a conditional block and returns true if any element meets the
# condition, false otherwise
def any?(&block)
erl.lists.any(block, self)
end

# Returns a new list built by concatenating the two lists together to
# produce a third list.
def append(other_list)
erl.lists.append(self, other_list)
end

# Iterate over a list, successively calling the given block and returning
# the receiver when done
def each(&block)
erl.lists.foreach(block, self)
self
end

# Is the list empty?
def empty?
self == []
end


# Returns the first element in the list # Returns the first element in the list
def first def first
Expand All @@ -38,40 +68,17 @@ class List
def first(n) def first(n)
erl.lists.sublist(self, n) erl.lists.sublist(self, n)
end end


# Returns the last element in the list # Flatten a deeply nested list
def last def flatten
erl.lists.nth(size(), self) erl.lists.flatten(self)
end

# returns the last n-elements in the list
def last(n)
index = [0, (size() - n)].max()
erl.lists.nthtail(index,self)
end

# Returns the first element of list that compares less than or equal
# to all other elements of list.
def min
erl.lists.min(self)
end

# Returns the first element of list that compares greater than or equal
# to all other elements of list.
def max
erl.lists.max(self)
end

# Number of elements in a list
def size
erl.length(self)
end end


# Reverse the order of a list # Generate a string representative of a list's contents
def reverse def inspect
erl.lists.reverse(self) to_s()
end end

# Join a list into a string, casting all elements to strings # Join a list into a string, casting all elements to strings
def join(separator) def join(separator)
elements = case self elements = case self
Expand All @@ -87,41 +94,33 @@ class List
join("") join("")
end end
# Returns a new list built by concatenating the two lists together to # Returns the last element in the list
# produce a third list. def last
def append(other_list) erl.lists.nth(size(), self)
erl.lists.append(self, other_list)
end

# Iterate over a list, successively calling the given block and returning
# the receiver when done
def each(&block)
erl.lists.foreach(block, self)
self
end end
# returns the last n-elements in the list
def last(n)
index = [0, (size() - n)].max()
erl.lists.nthtail(index,self)
end
# Iterate over a list, building a new list of values returned from the # Iterate over a list, building a new list of values returned from the
# given block when called with each element in the list # given block when called with each element in the list
def map(&block) def map(&block)
erl.lists.map(block, self) erl.lists.map(block, self)
end end
# Takes a conditional block and returns true if all elements meet the # Returns the first element of list that compares greater than or equal
# condition, false otherwise # to all other elements of list.
def all?(&block) def max
erl.lists.all(block, self) erl.lists.max(self)
end

# Takes a conditional block and returns true if any element meets the
# condition, false otherwise
def any?(&block)
erl.lists.any(block, self)
end end

# Takes a conditional block and returns a new list with only the elements # Returns the first element of list that compares less than or equal
# for which the block evaluates to true # to all other elements of list.
def select(&block) def min
erl.lists.filter(block, self) erl.lists.min(self)
end end
# Takes a conditional block and partitions the list into two lists, # Takes a conditional block and partitions the list into two lists,
Expand All @@ -132,31 +131,32 @@ class List
erl.lists.partition(block, self) erl.lists.partition(block, self)
end end
# Flatten a deeply nested list # Reverse the order of a list
def flatten def reverse
erl.lists.flatten(self) erl.lists.reverse(self)
end
# Takes a conditional block and returns a new list with only the elements
# for which the block evaluates to true
def select(&block)
erl.lists.filter(block, self)
end end
# Generate a string representative of a list's contents # Number of elements in a list
def inspect def size
to_s() erl.length(self)
end end
# Cast to a tuple # Cast to a binary
def to_tuple def to_binary
erl.list_to_tuple(self) erl.list_to_binary(self)
end end
# Cast a list of 2-tuples to a dict # Cast a list of 2-tuples to a dict
def to_dict def to_dict
erl.dict.from_list(self) erl.dict.from_list(self)
end end
# Cast to a binary
def to_binary
erl.list_to_binary(self)
end
# Cast to a list, returning the identity # Cast to a list, returning the identity
def to_list def to_list
self self
Expand All @@ -181,14 +181,9 @@ class List
"[#{map { |e| e.inspect() }.join(',')}]" "[#{map { |e| e.inspect() }.join(',')}]"
end end
# Is the list empty? # Cast to a tuple
def empty? def to_tuple
case self erl.list_to_tuple(self)
when []
true
when _
false
end
end end
# FIXME: implement private # FIXME: implement private
Expand Down

0 comments on commit 584be60

Please sign in to comment.