Skip to content

Commit 900d76f

Browse files
author
Badacadabra
committed
Add idiomatic style
1 parent c5cdd5e commit 900d76f

File tree

340 files changed

+4418
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

340 files changed

+4418
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
go = (nextRelay) ->
4+
res = ""
5+
if nextRelay? then res += nextRelay.go() else res += ""
6+
res
7+
8+
# ==============================
9+
# RACERS
10+
# ==============================
11+
12+
walker =
13+
go: ->
14+
"""
15+
Walker: go!
16+
#{go(@nextRelay)}
17+
"""
18+
19+
runner =
20+
go: ->
21+
"""
22+
Runner: go!
23+
#{go(@nextRelay)}
24+
"""
25+
26+
swimmer =
27+
go: ->
28+
"""
29+
Swimmer: go!
30+
#{go(@nextRelay)}
31+
"""
32+
33+
# ==============================
34+
# TEST
35+
# ==============================
36+
37+
walker.nextRelay = runner
38+
runner.nextRelay = swimmer
39+
40+
console.log walker.go()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict'
2+
3+
# ==============================
4+
# ORDER
5+
# ==============================
6+
7+
texMex =
8+
deliver: ->
9+
@customer.pay()
10+
11+
# ==============================
12+
# RESTAURANT
13+
# ==============================
14+
15+
restaurant =
16+
orders: []
17+
addOrder: (order) ->
18+
@orders.push order
19+
prepareOrders: ->
20+
summary = ""
21+
summary += order.deliver() for order in @orders
22+
summary
23+
24+
# ==============================
25+
# CUSTOMER
26+
# ==============================
27+
28+
customer =
29+
pay: ->
30+
"Payment OK!\n"
31+
32+
# ==============================
33+
# TEST
34+
# ==============================
35+
36+
texMex.customer = customer
37+
restaurant.addOrder texMex
38+
39+
console.log restaurant.prepareOrders()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict'
2+
3+
# ==============================
4+
# CONTEXT
5+
# ==============================
6+
7+
sonata =
8+
name: "Moonlight Sonata"
9+
composer: "Beethoven"
10+
11+
# ==============================
12+
# EXPRESSIONS
13+
# ==============================
14+
15+
# Terminal expressions
16+
note = (name) ->
17+
name: name
18+
interpret: (sonata) ->
19+
"#{sonata.composer} played #{@name}\n"
20+
21+
# Nonterminal expression
22+
arpeggio = (name) ->
23+
name: name
24+
notes: []
25+
addNotes: (notes) ->
26+
@notes.push note for note in notes
27+
interpret: (sonata) ->
28+
res = ""
29+
res += note.interpret sonata for note in @notes
30+
res += "This was a #{@name} arpeggio from #{sonata.name}.\n"
31+
res
32+
33+
# ==============================
34+
# TEST
35+
# ==============================
36+
37+
# To avoid overcomplexity, we only take into account the first measure and the right hand (G-clef)
38+
39+
# Notes
40+
gSharp = note "G#"
41+
cSharp = note "C#"
42+
e = note "E"
43+
44+
# Arpeggio
45+
cSharpMinor = arpeggio "C#m"
46+
cSharpMinor.addNotes [gSharp, cSharp, e]
47+
48+
# Melody
49+
melody = []
50+
51+
melody.push cSharpMinor for i in [1..4] # The same musical pattern is repeated four times...
52+
53+
# Interpretation
54+
console.log arpeggio.interpret sonata for arpeggio in melody
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict'
2+
3+
# ==============================
4+
# ITERATOR
5+
# ==============================
6+
7+
catalog =
8+
index: 0
9+
hasNext: ->
10+
@index < @collection.books.length
11+
next: ->
12+
@collection.books[@index++]
13+
14+
# ==============================
15+
# ITERABLE
16+
# ==============================
17+
18+
library =
19+
books: []
20+
21+
# ==============================
22+
# TEST
23+
# ==============================
24+
25+
# We get the iterator (catalog) from the iterable object (library)
26+
library.books = ["Foo", "Bar"]
27+
catalog.collection = library
28+
29+
console.log catalog.next() while catalog.hasNext()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
# ==============================
4+
# COLLEAGUES (NEIGHBORS)
5+
# ==============================
6+
7+
neighbor = (name) ->
8+
name: name
9+
send: (message, mediator) ->
10+
mediator.send message, this
11+
receive: (message, sender) ->
12+
"[#{@name}] Message from #{sender}: '#{message}'\n";
13+
14+
tom = neighbor "Tom"
15+
dick = neighbor "Dick"
16+
17+
# ==============================
18+
# MEDIATOR
19+
# ==============================
20+
21+
harry =
22+
tom: tom
23+
dick: dick
24+
send: (message, neighbor) ->
25+
if neighbor.name is "Tom"
26+
dick.receive message, "Tom"
27+
else if neighbor.name is "Dick"
28+
tom.receive message, "Dick"
29+
else
30+
throw
31+
type: "Not found",
32+
message: "The given person has not been found in the neighborhood."
33+
34+
# ==============================
35+
# TEST
36+
# ==============================
37+
38+
conversation = ""
39+
conversation += tom.send "Could you lower the volume of your music, please?", harry
40+
conversation += dick.send "Are you serious? The volume is actually very low...", harry
41+
42+
console.log conversation
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
# ==============================
4+
# ORIGINATOR & MEMENTO
5+
# ==============================
6+
7+
browser =
8+
saveCurrentPage: ->
9+
webpage: @currentPage
10+
11+
# ==============================
12+
# CARETAKER
13+
# ==============================
14+
15+
bookmarksManager =
16+
bookmarks: []
17+
addBookmark: (bookmark) ->
18+
@bookmarks.push bookmark
19+
20+
# ==============================
21+
# TEST
22+
# ==============================
23+
24+
browser.currentPage = "http://www.badacadabra.net"
25+
26+
bookmark = browser.saveCurrentPage()
27+
28+
bookmarksManager.addBookmark bookmark
29+
browser.currentPage = "https://www.google.com"
30+
browser.currentPage = "https://www.amazon.com"
31+
bookmark = browser.saveCurrentPage()
32+
bookmarksManager.addBookmark bookmark
33+
browser.currentPage = "https://www.twitter.com"
34+
35+
console.log "Current page: #{browser.currentPage}"
36+
console.log "Latest bookmark: #{bookmark.webpage}"
37+
console.log "First bookmark: #{bookmarksManager.bookmarks[0].webpage}"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
# ==============================
4+
# OBSERVERS
5+
# ==============================
6+
7+
lion =
8+
attack: ->
9+
"Lion attack!\n"
10+
11+
crocodile =
12+
attack: ->
13+
"Crocodile attack!\n"
14+
15+
# ==============================
16+
# OBSERVABLE
17+
# ==============================
18+
19+
gazelle =
20+
predators: []
21+
addPredator: (predator) ->
22+
@predators.push predator
23+
notifyPredators: ->
24+
situation = ""
25+
situation += predator.attack() for predator in @predators
26+
situation
27+
28+
# ==============================
29+
# TEST
30+
# ==============================
31+
32+
gazelle.addPredator lion
33+
gazelle.addPredator crocodile
34+
35+
console.log gazelle.notifyPredators()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict'
2+
3+
# ==============================
4+
# STATES
5+
# ==============================
6+
7+
# "off" and "on" are reserved keywords in CoffeeScript
8+
9+
OFF =
10+
name: "off"
11+
power: (pc) ->
12+
pc.currentState = pc.states.ON
13+
14+
ON =
15+
name: "on"
16+
power: (pc) ->
17+
pc.currentState = pc.states.OFF
18+
19+
# ==============================
20+
# CONTEXT
21+
# ==============================
22+
23+
pc =
24+
currentState: null
25+
states:
26+
OFF: OFF
27+
ON: ON
28+
power: ->
29+
@currentState.power(@)
30+
31+
# ==============================
32+
# TEST
33+
# ==============================
34+
35+
pc.currentState = pc.states.OFF
36+
37+
console.log pc.currentState.name # off
38+
pc.power()
39+
console.log pc.currentState.name # on
40+
pc.power()
41+
console.log pc.currentState.name # off
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
# ==============================
4+
# CONTEXT
5+
# ==============================
6+
7+
fightingGame =
8+
strategy: null
9+
play: ->
10+
@strategy.fight()
11+
12+
# ==============================
13+
# STRATEGIES
14+
# ==============================
15+
16+
offense =
17+
fight: ->
18+
"Fight with an offensive style"
19+
20+
defense =
21+
fight: ->
22+
"Fight with a defensive style"
23+
24+
# ==============================
25+
# TEST
26+
# ==============================
27+
28+
fightingGame.strategy = defense
29+
console.log "ROUND 1 - #{fightingGame.play()}"
30+
fightingGame.strategy = offense
31+
console.log "ROUND 2 - #{fightingGame.play()}"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
# ==============================
4+
# TEMPLATE METHOD
5+
# ==============================
6+
7+
build = (construction) ->
8+
"""
9+
Construction of a new home:
10+
1. #{construction.foundations()}
11+
2. #{construction.walls()}
12+
3. #{construction.roof()}\n
13+
"""
14+
15+
# ==============================
16+
# HOMES
17+
# ==============================
18+
19+
house =
20+
foundations: ->
21+
"House foundations"
22+
walls: ->
23+
"House walls"
24+
roof: ->
25+
"House roof"
26+
27+
building =
28+
foundations: ->
29+
"Apartment building foundations"
30+
walls: ->
31+
"Apartment building walls"
32+
roof: ->
33+
"Apartment building roof"
34+
35+
# ==============================
36+
# TEST
37+
# ==============================
38+
39+
console.log build house
40+
console.log build building

0 commit comments

Comments
 (0)