Example #1
0
 public static void Apply(LispList <T> list, Action <T> action)
 {
     for (; list != null; list = list.tail)
     {
         action(list.Head);
     }
 }
Example #2
0
 public static LispList <S> Select <S> (LispList <T> list, Func <T, S> selector)
 {
     if (list == null)
     {
         return(null);
     }
     return(list.tail.Select(selector).Cons(selector(list.Head)));
 }
Example #3
0
 public static int LengthOf(LispList <T> list)
 {
     if (list == null)
     {
         return(0);
     }
     return(list.count);
 }
Example #4
0
        public IImmutableMap <K, V> Add(K key, V value)
        {
            int hashCode = key.GetHashCode();
            LispList <Pair <K, V> > list1   = this.immutable_int_map [hashCode];
            LispList <Pair <K, V> > newList = Remove(list1, key).Cons(new Pair <K, V> (key, value));
            int          diff    = newList.Length() - list1.Length();
            LispList <K> newKeys = diff == 0 ? this.keys : this.keys.Cons(key);

            return(new ImmutableMap <K, V> (this.immutable_int_map.Add(hashCode, newList), this.count + diff, newKeys));
        }
Example #5
0
        public static LispList <T> Reverse(LispList <T> list)
        {
            LispList <T> rest = null;

            for (; list != null; list = list.tail)
            {
                rest = rest.Cons(list.element);
            }
            return(rest);
        }
Example #6
0
        public static IEnumerable <T> PrivateGetEnumerable(LispList <T> list)
        {
            LispList <T> current = list;

            while (current != null)
            {
                T next = current.Head;
                current = current.tail;
                yield return(next);
            }
        }
Example #7
0
        public static LispList <T> Append <T> (this LispList <T> list, LispList <T> append)
        {
            if (list == null)
            {
                return(append);
            }
            if (append == null)
            {
                return(list);
            }

            return(Cons(list.Tail.Append(append), list.Head));
        }
Example #8
0
        private static LispList <K> RemoveKey(K key, LispList <K> keys)
        {
            if (keys == null)
            {
                throw new InvalidOperationException();
            }

            if (key.Equals(keys.Head))
            {
                return(keys.Tail);
            }

            return(RemoveKey(key, keys.Tail).Cons(keys.Head));
        }
Example #9
0
        private LispList <Pair <K, V> > Remove(LispList <Pair <K, V> > from, K key)
        {
            if (from == null)
            {
                return(null);
            }
            if (key.Equals(from.Head.Key))
            {
                return(from.Tail);
            }
            LispList <Pair <K, V> > tail = Remove(from.Tail, key);

            return(tail == from.Tail ? from : tail.Cons(from.Head));
        }
Example #10
0
        public static T Last <T> (this LispList <T> list)
        {
            if (list == null)
            {
                return(default(T));
            }

            while (LispList <T> .LengthOf(list) > 1)
            {
                list = list.Tail;
            }

            return(list.Head);
        }
Example #11
0
        public static bool Any <T> (this LispList <T> list, Predicate <T> predicate)
        {
            if (list == null)
            {
                return(false);
            }

            if (predicate(list.Head))
            {
                return(true);
            }

            return(list.Tail.Any(predicate));
        }
Example #12
0
 public V this [K key]
 {
     get
     {
         for (LispList <Pair <K, V> > list = this.immutable_int_map [key.GetHashCode()]; list != null; list = list.Tail)
         {
             K k = list.Head.Key;
             if (key.Equals(k))
             {
                 return(list.Head.Value);
             }
         }
         return(default(V));
     }
 }
Example #13
0
        public static LispList <T> Where <T> (this LispList <T> list, Predicate <T> keep)
        {
            if (list == null)
            {
                return(null);
            }
            LispList <T> rest = list.Tail.Where(keep);

            if (!keep(list.Head))
            {
                return(rest);
            }

            if (rest == list.Tail)
            {
                return(list);
            }

            return(Cons(rest, list.Head));
        }
Example #14
0
        public static bool Contains(LispList <T> l, T o)
        {
            if (l == null)
            {
                return(false);
            }
            var equatable = o as IEquatable <T>;

            if (equatable != null)
            {
                if (equatable.Equals(l.element))
                {
                    return(true);
                }
            }
            else if (o.Equals(l.element))
            {
                return(true);
            }

            return(Contains(l.tail, o));
        }
Example #15
0
        public IImmutableMap <K, V> Remove(K key)
        {
            int hashCode = key.GetHashCode();
            LispList <Pair <K, V> > from = this.immutable_int_map [hashCode];

            if (from == null)
            {
                return(this);
            }
            LispList <Pair <K, V> > newList = Remove(from, key);

            if (newList == from)
            {
                return(this);
            }
            LispList <K> newKeys = RemoveKey(key, this.keys);

            if (newList == null)
            {
                return(new ImmutableMap <K, V> (this.immutable_int_map.Remove(hashCode), this.count - 1, newKeys));
            }
            return(new ImmutableMap <K, V> (this.immutable_int_map.Add(hashCode, newList), this.count - 1, newKeys));
        }
Example #16
0
 public static LispList <T> Cons <T> (this LispList <T> rest, T elem)
 {
     return(LispList <T> .Cons(elem, rest));
 }
Example #17
0
 public static LispList <T> Coerce <S, T> (this LispList <S> list)
     where S : T
 {
     return(list.Select(l => (T)l));
 }
Example #18
0
 private LispList(T elem, LispList <T> tail)
 {
     this.element = elem;
     this.tail    = tail;
     this.count   = LengthOf(tail) + 1;
 }
Example #19
0
 public static LispList <S> Select <T, S> (this LispList <T> list, Func <T, S> selector)
 {
     return(LispList <T> .Select(list, selector));
 }
Example #20
0
 private ImmutableMap(IImmutableIntMap <LispList <Pair <K, V> > > map, int count, LispList <K> keys)
 {
     this.keys              = keys;
     this.count             = count;
     this.immutable_int_map = map;
 }
Example #21
0
 public static void Apply <T> (this LispList <T> list, Action <T> action)
 {
     LispList <T> .Apply(list, action);
 }
Example #22
0
 public static IEnumerable <T> AsEnumerable <T> (this LispList <T> list)
 {
     return(LispList <T> .PrivateGetEnumerable(list));
 }
Example #23
0
 public static bool IsEmpty <T> (this LispList <T> list)
 {
     return(list == null);
 }
Example #24
0
 public static int Length <T> (this LispList <T> list)
 {
     return(LispList <T> .LengthOf(list));
 }
Example #25
0
 public static LispList <T> Cons(T elem, LispList <T> tail)
 {
     return(new LispList <T> (elem, tail));
 }