Skip to content

Commit

Permalink
Cleaning up examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kengorab committed Mar 14, 2021
1 parent 23164aa commit 3ffa4ab
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 124 deletions.
13 changes: 11 additions & 2 deletions src/pages/PlaygroundPage/examples/enums.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export default `
export default {
label: 'Enums',
modules: [
{
name: '.enums',
label: 'enums.abra',
code: `
enum Color {
Red
Green
Expand All @@ -13,4 +19,7 @@ enum Color {
}
Color.Red.hexCode()
`.trimStart()
`.trimStart()
}
]
}
13 changes: 11 additions & 2 deletions src/pages/PlaygroundPage/examples/fibonacci.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export default `
export default {
label: 'Fibonacci',
modules: [
{
name: '.fibonacci',
label: 'fibonacci.abra',
code: `
func fib(n: Int): Int {
if (n == 0) {
0
Expand All @@ -10,4 +16,7 @@ func fib(n: Int): Int {
}
fib(12)
`.trimStart()
`.trimStart()
}
]
}
13 changes: 11 additions & 2 deletions src/pages/PlaygroundPage/examples/fizzbuzz.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export default `
export default {
label: 'Fizzbuzz',
modules: [
{
name: '.fizzbuzz',
label: 'fizzbuzz.abra',
code: `
for a in range(1, 101) {
if a % 15 == 0 {
println("Fizzbuzz")
Expand All @@ -9,4 +15,7 @@ for a in range(1, 101) {
} else {
println(a)
}
}`.trimStart()
}`.trimStart()
}
]
}
15 changes: 12 additions & 3 deletions src/pages/PlaygroundPage/examples/greeting.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
export default `
export default {
label: 'Greeting',
modules: [
{
name: '.greeting',
label: 'greeting.abra',
code: `
val greeting = "Hello"
func greet(recipient: String) = greeting + ", " + recipient
func greet(recipient: String): String = greeting + ", " + recipient
val languageName = "Abra"
greet(languageName)
`.trimStart()
`.trimStart()
}
]
}
82 changes: 82 additions & 0 deletions src/pages/PlaygroundPage/examples/imports-exports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
export default {
label: 'Imports/Exports',
modules: [
{
name: '.main',
label: 'main.abra',
code: `
import Ship from .ship
import Person from .person
import prettyPrint from .util
val ship = Ship(
name: "Planet Express Ship",
crew: [
Person(firstName: "Turanga", lastName: "Leela"),
Person(firstName: "Phillip", middleInitial: "J", lastName: "Fry"),
Person(firstName: "Bender", middleInitial: "B", lastName: "Rodriguez"),
]
)
println(prettyPrint(ship))
`.trimStart()
},
{
name: '.person',
label: 'person.abra',
code: `
export type Person {
firstName: String
middleInitial: String? = None
lastName: String
}
`.trimStart()
},
{
name: '.ship',
label: 'ship.abra',
code: `
import Person from .person
export type Ship {
name: String
crew: Person[]
}
`.trimStart()
},
{
name: '.util',
label: 'util.abra',
code: `
import Ship from .ship
import Person from .person
// Ugly prettyPrint function, nothing to see here 🙈
export func prettyPrint(ship: Ship): String {
var str = "Ship(\\n"
str += " name: \\"\${ship.name}\\",\\n"
str += " crew: [\\n"
for person, idx in ship.crew {
str += prettyPrintPerson(person, " ")
if idx != ship.crew.length - 1 {
str += ","
}
str += "\\n"
}
str += " ]\\n"
str += ")"
}
func prettyPrintPerson(person: Person, indent = ""): String {
var str = "\${indent}Person(\\n"
str += "$indent firstName: \\"\${person.firstName}\\",\\n"
if person.middleInitial |i| {
str += "$indent middleInitial: \\"$i\\",\\n"
}
str += "$indent lastName: \\"\${person.lastName}\\",\\n"
str += "$indent)"
}
`.trimStart()
}
]
}
119 changes: 7 additions & 112 deletions src/pages/PlaygroundPage/examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import importsExports from './imports-exports'
import greeting from './greeting'
import fibonacci from './fibonacci'
import fizzbuzz from './fizzbuzz'
Expand All @@ -16,118 +17,12 @@ export interface Example {
}

const codeExamples: Example[] = [
{
label: 'Greeting',
modules: [
{ name: '.greeting', label: 'greeting.abra', code: greeting }
]
},
{
label: 'Fibonacci',
modules: [
{ name: '.fibonacci', label: 'fibonacci.abra', code: fibonacci }
]
},
{
label: 'Fizzbuzz',
modules: [
{ name: '.fizzbuzz', label: 'fizzbuzz.abra', code: fizzbuzz }
]
},
{
label: 'Linked List (ish)',
modules: [
{ name: '.linked_list', label: 'linked_list.abra', code: linkedList }
]
},
{
label: 'Enums',
modules: [
{ name: '.enums', label: 'enums.abra', code: enums }
]
},
{
label: 'Imports/Exports',
modules: [
{
name: '.main',
label: 'main.abra',
code: `
import Ship from .ship
import Person from .person
import prettyPrint from .util
val ship = Ship(
name: "Planet Express Ship",
crew: [
Person(firstName: "Turanga", lastName: "Leela"),
Person(firstName: "Phillip", middleInitial: "J", lastName: "Fry"),
Person(firstName: "Bender", middleInitial: "B", lastName: "Rodriguez"),
]
)
println(prettyPrint(ship))
`.trimStart()
},
{
name: '.person',
label: 'person.abra',
code: `
export type Person {
firstName: String
middleInitial: String? = None
lastName: String
}
`.trimStart()
},
{
name: '.ship',
label: 'ship.abra',
code: `
import Person from .person
export type Ship {
name: String
crew: Person[]
}
`.trimStart()
},
{
name: '.util',
label: 'util.abra',
code: `
import Ship from .ship
import Person from .person
// Ugly prettyPrint function, nothing to see here 🙈
export func prettyPrint(ship: Ship): String {
var str = "Ship(\\n"
str += " name: \\"\${ship.name}\\",\\n"
str += " crew: [\\n"
for person, idx in ship.crew {
str += prettyPrintPerson(person, " ")
if idx != ship.crew.length - 1 {
str += ","
}
str += "\\n"
}
str += " ]\\n"
str += ")"
}
func prettyPrintPerson(person: Person, indent = ""): String {
var str = "\${indent}Person(\\n"
str += "$indent firstName: \\"\${person.firstName}\\",\\n"
if person.middleInitial |i| {
str += "$indent middleInitial: \\"$i\\",\\n"
}
str += "$indent lastName: \\"\${person.lastName}\\",\\n"
str += "$indent)"
}
`.trimStart()
}
]
}
importsExports,
greeting,
fibonacci,
fizzbuzz,
linkedList,
enums
]

export default codeExamples
13 changes: 11 additions & 2 deletions src/pages/PlaygroundPage/examples/linked-list.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export default `
export default {
label: 'Linked List (ish)',
modules: [
{
name: '.linked_list',
label: 'linked_list.abra',
code: `
type Node<T> {
value: T
next: Node<T>? = None
Expand Down Expand Up @@ -45,4 +51,7 @@ list.push("a")
.push("ghij")
list.map<Int>(item => item.length)
`.trimStart()
`.trimStart()
}
]
}
2 changes: 1 addition & 1 deletion src/pages/PlaygroundPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface State {
isError: boolean,
}

const STARTING_EXAMPLE = examples[5]
const STARTING_EXAMPLE = examples[0]

export default class PlaygroundPage extends React.Component<{}, State> {
editorRef = React.createRef<CodeMirrorEditor>()
Expand Down

0 comments on commit 3ffa4ab

Please sign in to comment.