summaryrefslogtreecommitdiff
path: root/hw5/hw5.moon
blob: 142625cdec24222f47a31961bcffd43cec8527c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
--Homework 5, Alexander Pickering (amp215@pitt.edu)
--
-- Moonscript!
-- $ apt-get install lua5.3 lua5.3-dev luarocks
-- $ luarocks install moonscript
-- 
-- You can then run this file with
-- $ moon hw5.moon

accept_input = io.read

--seperate things that are one or more non-space characters
--https://www.lua.org/manual/5.3/manual.html#6.4.1
split_string = (s) -> [x for x in string.gmatch(s,"(%S+)")] 

--apply string.upper() to the first character in a word
capitalize_word = (word) -> string.gsub(word,"^(.)",string.upper)

--uppercase words in an array
uppercase_words = (array) ->
	[capitalize_word(word) for word in *array] 

--print all the words in an array
print_words = (array) ->
	for word in *array do
		print(word)

--Built in sorting!
sort_words = table.sort

--If a function has no arguments, it can be called using the ! operator, instead of empty parentheses. The ! invocation is the preferred way to call functions with no arguments.
sentence = accept_input!

words = split_string(sentence)
words = uppercase_words(words)
sort_words(words)

print_words(words)