Last active
July 20, 2018 07:02
-
-
Save thosakwe/0558bb9038751d8b58e5096d1541eca0 to your computer and use it in GitHub Desktop.
MANDA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Snake { | |
| var name: String | |
| get title: String { | |
| return '%name the Legendary Snake' | |
| } | |
| set age (value: Num) { | |
| } | |
| } | |
| // Constructors are regular functions | |
| let manda = Snake() | |
| manda.name = 'Manda' | |
| print('Hello, %{manda.title}!'); | |
| // You can construct a class with initial values for fields. | |
| let orochimaru = Snake { name: 'Orochimaru' } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with Curl | |
| let curl = new Curl() | |
| curl.get('https://github.com/thosakwe').ok(func (res) { | |
| print('Status: %{res.statusCode}') | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let x = 23 | |
| let dict = {x, a: 'b'} | |
| print('23 = %{dict.x}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <manda.h> | |
| #include <stdint.h> | |
| void my_extension_init(manda_context_t*, ctx, manda_object_t* object) { | |
| manda_set_property(ctx, object, "isEven", manda_wrap_function(isEven)); | |
| } | |
| manda_object_t* isEven(manda_context_t* ctx, manda_arguments_t* args) { | |
| int64_t x; | |
| manda_object_t* x_object = manda_get_argument(ctx, args, 0); | |
| if (manda_num_to_int64(ctx, x_object, &x) == 0) { | |
| return manda_bool(ctx, x % 2 == 0); | |
| } else { | |
| return manda_last_error(ctx); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ffi | |
| let lib = Ffi.open('my_extension') | |
| func nativeIsEven(x: Num): Bool { | |
| return lib.isEven(x) | |
| } | |
| print(nativeIsEven(x: 45) == True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func implicitAny(x) { | |
| return 45 + x | |
| } | |
| func explicitTypes(x: Num): Num { | |
| return 45 + x | |
| } | |
| let eighty = explicitTypes(35) | |
| print('Eighty: %eighty'); | |
| // You can also pass parameters by name: | |
| let ninety = explicitTypes(x: 45) | |
| print('Ninety: %ninety') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func asyncIsEven (x: Num): Future<Bool> { | |
| return Future(func (ok, fail) { | |
| return ok(x % 2 == 0) | |
| }) | |
| } | |
| asyncIsEven(x: 24).ok(func (isEven) { | |
| print('Even? %isEven') | |
| }) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print('Hello, world!') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class HasLegs { | |
| // Subclasses must override this | |
| virtual get legCount: Num | |
| travel(): void { | |
| print('I use my legs to travel!') | |
| } | |
| } | |
| class Automobile { | |
| virtual get wheelCount: Num | |
| travel(): void { | |
| print('I use my wheels to travel!') | |
| } | |
| } | |
| /// A robot in disguise. | |
| class Transformer : Automobile, HasLegs { | |
| constructor(property name: String); | |
| // The `override` keyword is ignored; it is purely syntactic | |
| // sugar to make Manda more readable. | |
| override get legCount: Num => 2 | |
| override get wheelCount: Num => 4 | |
| } | |
| let optimusPrime = Transformer(name: 'Optimus Prime') | |
| // Classes in Manda can extend multiple parents; in the case of a naming conflict, | |
| // the first alternative is prioritized. | |
| // | |
| // In this case, the statement 'I use my wheels to travel!' is printed. | |
| optimusPrime.travel() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Json | |
| let obj = Json.parse('{"foo": "bar"}') as {foo: String} | |
| print('Foo: %foo') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for (i in [0 .. 10]) { | |
| print('Hey!') | |
| } | |
| for (i: Num in [0 .. 10]) { | |
| print('It\'s a number! %i') | |
| } | |
| while (True) { | |
| // Goes on forever... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Math | |
| func curve (x: Num): Num => pow(x, 2) + 4 | |
| print(curve(34)) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func ambivalent(x: Num): String? { | |
| if (x % 2 == 0) { | |
| return 'Yes!' | |
| } else { | |
| return Null | |
| } | |
| } | |
| // Use the `!` operator to assert an expression is non-null; otherwise throws. | |
| var str = ambivalent(x: 44) | |
| print(str!.length) | |
| // Use the `?` operator only evaluate the remainder of a member (ex. `a.b`) expression if it does not | |
| // evaluate to `Null`. | |
| str = ambivalent(x: 45) | |
| print(str?.length ?? 'Odd number') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Redis | |
| let redis = Redis.connect('127.0.0.1', 6379) | |
| let reply = redis.send('SET foo bar') | |
| print('Success: %{reply.isSuccess}') | |
| redis.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Net | |
| import Text | |
| let server = Net.Socket.server() | |
| do { | |
| try server.bind('127.0.0.1', 3000) | |
| while (True) { | |
| let sock = server.accept() | |
| print('FD: %{sock.fileDescriptor}') | |
| let printer = Text.Printer(sock) | |
| printer.line('Hello, Manda world!!!') | |
| sock.close() | |
| } | |
| } catch(error) { | |
| print('Whoops: %error') | |
| } finally { | |
| server.close() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with Async | |
| func simpleStream(): Stream<Num> { | |
| return Stream.fromList([1, 2, 3]) | |
| } | |
| func controlledStream(): Stream<Num> { | |
| let ctrl = StreamController() | |
| ctrl.addAll([1, 2, 3]) | |
| return ctrl.stream | |
| } | |
| // Stream of 6 numbers: 1, 2, 3, 1, 2, 3 | |
| let numberStream = Stream.concat([simpleStream(), controlledStream()]) | |
| // Print every number that comes out of the stream. | |
| numberStream.listen(print) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func doSomething() { | |
| throw Error('I made a boo-boo... :/') | |
| } | |
| // Syntax just like Swift's; use a `do` block for cleaner error handling. | |
| // | |
| // Use the `try` operator to handle a potential error. | |
| do { | |
| let user = try doSomething() | |
| print('User: %user') | |
| } catch(error) { | |
| print('Uh-oh: %{error.message}') | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type HasFoo = {foo: String} | |
| type SomeType = Num | String | HasFoo | |
| func doIt(value: SomeType): void { | |
| if (value is Num) { | |
| print('Num: %value') | |
| } else if (value is String) { | |
| print('String: %value') | |
| } else if (value is HasFoo) { | |
| print('HasFoo; foo=%{value.foo}') | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let x = 2 * 3 | |
| let y = x * x | |
| var z = y * x - 1 | |
| z = 34.5 | |
| print('x: %x, y: %y, %z') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment