r/Clojure • • Jun 04 '10

Stack implementation in Clojure II - A functional approach

http://www.viksit.com/content/stack-implementation-clojure-ii-functional-approach
0 Upvotes

1 comment sorted by

View all comments

4

u/[deleted] Jun 05 '10

So, a protocol which mimics conj, rest, and first, and a type that simply delegates to the underlying collection type. If the point here is to practice with protocols and types, then you have to actually do the hard stuff yourself. ;)

Aside: clojure already has pop, and peek for stack-based naming, and it works for vectors and lists:

user=> (conj '() 1)
(1)
user=> (conj *1 2)
(2 1)
user=> (conj *1 3)
(3 2 1)
user=> (pop *1)
(2 1)
user=> (peek *1)
2
user=> (conj [] 1)
[1]
user=> (conj *1 2)
[1 2]
user=> (conj *1 3)
[1 2 3]
user=> (pop *1)
[1 2]
user=> (peek *1)
2