Skip to content

Commit

Permalink
make, ps, ruby: add seq/string?
Browse files Browse the repository at this point in the history
Issue #166
  • Loading branch information
kanaka committed Feb 11, 2016
1 parent 6791e64 commit a968e28
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
46 changes: 34 additions & 12 deletions make/core.mk
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ time_ms = $(call _number,$(shell echo $$(date +%s%3N)))

# String functions

string? = $(if $(call _string?,$(1)),$(__true),$(__false))
string? = $(if $(call _string?,$(1)),$(if $(call _keyword?,$(1)),$(__false),$(__true)),$(__false))

pr_str = $(call _string,$(call _pr_str_mult,$(1),yes, ))
str = $(call _string,$(call _pr_str_mult,$(1),,))
Expand Down Expand Up @@ -143,14 +143,6 @@ empty? = $(if $(call _EQ,0,$(if $(call _hash_map?,$(1)),$($(1)_size),$(words $($

count = $(call _number,$(call _count,$(1)))

conj = $(word 1,$(foreach new_list,$(call __new_obj_like,$(word 1,$(1))),\
$(new_list) \
$(eval $(new_list)_value := $(strip $($(word 1,$(1))_value))) \
$(if $(call _list?,$(new_list)),\
$(foreach elem,$(wordlist 2,$(words $(1)),$(1)),\
$(eval $(new_list)_value := $(strip $(elem) $($(new_list)_value)))),\
$(eval $(new_list)_value := $(strip $($(new_list)_value) $(wordlist 2,$(words $(1)),$(1)))))))

# Creates a new vector/list of the everything after but the first
# element
srest = $(word 1,$(foreach new_list,$(call _list),\
Expand All @@ -161,7 +153,7 @@ srest = $(word 1,$(foreach new_list,$(call _list),\
# (function object) using the remaining arguments.
sapply = $(call $(word 1,$(1))_value,$(strip \
$(wordlist 2,$(call int_sub,$(words $(1)),1),$(1)) \
$($(word $(words $(1)),$(1))_value)))
$($(word $(words $(1)),$(1))_value)))

# Map a function object over a list object
smap = $(strip\
Expand All @@ -175,6 +167,34 @@ smap = $(strip\
$(call $(func)_value,$(val))))))\
$(__obj_magic)_$(type)_$(new_hcode))))))

conj = $(word 1,$(foreach new_list,$(call __new_obj_like,$(word 1,$(1))),\
$(new_list) \
$(eval $(new_list)_value := $(strip $($(word 1,$(1))_value))) \
$(if $(call _list?,$(new_list)),\
$(foreach elem,$(wordlist 2,$(words $(1)),$(1)),\
$(eval $(new_list)_value := $(strip $(elem) $($(new_list)_value)))),\
$(eval $(new_list)_value := $(strip $($(new_list)_value) $(wordlist 2,$(words $(1)),$(1)))))))

seq = $(strip\
$(if $(call _list?,$(1)),\
$(if $(call _EQ,0,$(call _count,$(1))),$(__nil),$(1)),\
$(if $(call _vector?,$(1)),\
$(if $(call _EQ,0,$(call _count,$(1))),\
$(__nil),\
$(word 1,$(foreach new_list,$(call _list),\
$(new_list) \
$(eval $(new_list)_value := $(strip $($(word 1,$(1))_value)))))),\
$(if $(call _EQ,string,$(call _obj_type,$(1))),\
$(if $(call _EQ,0,$(call _count,$(1))),\
$(__nil),\
$(word 1,$(foreach new_list,$(call _list),\
$(new_list) \
$(eval $(new_list)_value := $(strip \
$(foreach c,$($(word 1,$(1))_value),\
$(call _string,$(c)))))))),\
$(if $(call _nil?,$(1)),\
$(__nil),\
$(call _error,seq: called on non-sequence))))))

# Metadata functions

Expand Down Expand Up @@ -214,12 +234,12 @@ core_ns = type obj_type \
nil? nil? \
true? true? \
false? false? \
string? string? \
symbol symbol \
symbol? symbol? \
keyword keyword \
keyword? keyword? \
function? function? \
string? string? \
\
pr-str pr_str \
str str \
Expand Down Expand Up @@ -262,10 +282,12 @@ core_ns = type obj_type \
last slast \
empty? empty? \
count count \
conj conj \
apply sapply \
map smap \
\
conj conj \
seq seq \
\
with-meta with_meta \
meta meta \
atom atom \
Expand Down
Binary file modified ps/core.ps
Binary file not shown.
Binary file modified ps/types.ps
Binary file not shown.
5 changes: 4 additions & 1 deletion ruby/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
:nil? => lambda {|a| a == nil},
:true? => lambda {|a| a == true},
:false? => lambda {|a| a == false},
:string? => lambda {|a| (a.is_a? String) && "\u029e" != a[0]},
:symbol => lambda {|a| a.to_sym},
:symbol? => lambda {|a| a.is_a? Symbol},
:keyword => lambda {|a| "\u029e"+a},
Expand Down Expand Up @@ -51,10 +52,12 @@
:rest => lambda {|a| List.new(a.nil? || a.size == 0 ? [] : a.drop(1))},
:empty? => lambda {|a| a.size == 0},
:count => lambda {|a| return 0 if a == nil; a.size},
:conj => lambda {|*a| a[0].clone.conj(a.drop(1))},
:apply => lambda {|*a| a[0][*a[1..-2].concat(a[-1])]},
:map => lambda {|a,b| List.new(b.map {|e| a[e]})},

:conj => lambda {|*a| a[0].clone.conj(a.drop(1))},
:seq => lambda {|a| a.nil? ? nil : a.size == 0 ? nil : a.seq},

:"with-meta" => lambda {|a,b| x = a.clone; x.meta = b; x},
:meta => lambda {|a| a.meta},
:atom => lambda {|a| Atom.new(a)},
Expand Down
12 changes: 12 additions & 0 deletions ruby/types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ def initialize(data)
end
end

class String # re-open and add seq
def seq()
return List.new self.split("")
end
end

class List < Array
attr_accessor :meta
def conj(xs)
xs.each{|x| self.unshift(x)}
return self
end
def seq()
return self
end
end

class Vector < Array
Expand All @@ -21,6 +30,9 @@ def conj(xs)
self.push(*xs)
return self
end
def seq()
return List.new self
end
end

class Hash # re-open and add meta
Expand Down

0 comments on commit a968e28

Please sign in to comment.