# Path: joy/parser.py
# def text_to_expression(text):
#   '''
#   Convert a text to a Joy expression.
#   '''
#   return _parse(_tokenize(text))
# 
# class Symbol(str):
#   __repr__ = str.__str__
# 
# Path: joy/utils/stack.py
# def list_to_stack(el, stack=()):
#   '''Convert a list (or other sequence) to a stack.
# 
#   [1, 2, 3] -> (1, (2, (3, ())))
# 
#   '''
#   for item in reversed(el):
#     stack = item, stack
#   return stack
# 
# def iter_stack(stack):
#   '''Iterate through the items on the stack.'''
#   while stack:
#     item, stack = stack
#     yield item
# 
# def pick(s, n):
#   '''
#   Find the nth item on the stack. (Pick with zero is the same as "dup".)
#   '''
#   if n < 0:
#     raise ValueError
#   while True:
#     try:
#       item, s = s
#     except ValueError:
#       raise IndexError
#     n -= 1
#     if n < 0:
#       break
#   return item
# 
# def pushback(quote, expression):
#   '''Concatinate quote onto expression.
# 
#   In joy [1 2] [3 4] would become [1 2 3 4].
#   '''
#   return list_to_stack(list(iter_stack(quote)), expression)


from inspect import getdoc
from .parser import text_to_expression, Symbol
from .utils.stack import list_to_stack, iter_stack, pick, pushback
import operator, math
def sum_(S):
  '''Given a quoted sequence of numbers return the sum.

  sum == 0 swap [+] step
  '''
  tos, stack = S
  return sum(iter_stack(tos)), stack


def remove(S):
  '''
  Expects an item on the stack and a quote under it and removes that item
  from the the quote.  The item is only removed once.

     [1 2 3 1] 1 remove
  ------------------------
          [2 3 1]

  '''
  (tos, (second, stack)) = S
  l = list(iter_stack(second))
  l.remove(tos)
  return list_to_stack(l), stack


def unique(S):
  '''Given a list remove duplicate items.'''
  tos, stack = S
  I = list(iter_stack(tos))
  list_to_stack(sorted(set(I), key=I.index))

# Path: joy/parser.py
# def text_to_expression(text):
#   '''
#   Convert a text to a Joy expression.
#   '''
#   return _parse(_tokenize(text))
# 
# class Symbol(str):
#   __repr__ = str.__str__
# 
# Path: joy/utils/stack.py
# def list_to_stack(el, stack=()):
#   '''Convert a list (or other sequence) to a stack.
# 
#   [1, 2, 3] -> (1, (2, (3, ())))
# 
#   '''
#   for item in reversed(el):
#     stack = item, stack
#   return stack
# 
# def iter_stack(stack):
#   '''Iterate through the items on the stack.'''
#   while stack:
#     item, stack = stack
#     yield item
# 
# def pick(s, n):
#   '''
#   Find the nth item on the stack. (Pick with zero is the same as "dup".)
#   '''
#   if n < 0:
#     raise ValueError
#   while True:
#     try:
#       item, s = s
#     except ValueError:
#       raise IndexError
#     n -= 1
#     if n < 0:
#       break
#   return item
# 
# def pushback(quote, expression):
#   '''Concatinate quote onto expression.
# 
#   In joy [1 2] [3 4] would become [1 2 3 4].
#   '''
#   return list_to_stack(list(iter_stack(quote)), expression)


# Path: joy/library.py
from inspect import getdoc
from .parser import text_to_expression, Symbol
from .utils.stack import list_to_stack, iter_stack, pick, pushback
import operator, math

def sum_(S):
  '''Given a quoted sequence of numbers return the sum.

  sum == 0 swap [+] step
  '''
  tos, stack = S
  return sum(iter_stack(tos)), stack


def remove(S):
  '''
  Expects an item on the stack and a quote under it and removes that item
  from the the quote.  The item is only removed once.

     [1 2 3 1] 1 remove
  ------------------------
          [2 3 1]

  '''
  (tos, (second, stack)) = S
  l = list(iter_stack(second))
  l.remove(tos)
  return list_to_stack(l), stack


def unique(S):
  '''Given a list remove duplicate items.'''
  tos, stack = S
  I = list(iter_stack(tos))
  list_to_stack(sorted(set(I), key=I.index))

  return list_to_stack(sorted(set(I), key=I.index)), stack