Skip to content

initial reason implementation#127

Open
songz wants to merge 13 commits intollipio:masterfrom
songz:reason
Open

initial reason implementation#127
songz wants to merge 13 commits intollipio:masterfrom
songz:reason

Conversation

@songz
Copy link
Collaborator

@songz songz commented Sep 15, 2017

No description provided.

Copy link
Collaborator

@yjlim5 yjlim5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool stuff!

src/demo.re Outdated
| Er
| Ir;

let getSubjectVariant subject => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically in Reason you would group together a type and its main operations into a single module. E.g.:

module Subject = {
  /* The type name becomes just `t` to avoid repetition when accessed from outside the module */
  type t =
    | Yo
    | Tu
    | Ella
    | Nosotras
    | Vosotras
    | Ellas;
  let ofString =
    fun
    | "yo" => Yo
    | "tu" => Tu
    | "ella" => Ella
    | "nosotras" => Nosotras
    | "vosotras" => Vosotras
    | _ => Ellas;
};

... and similarly for the ending type. Then you'd be able to use them like Subject.ofString, Ending.ofString. It makes the API uniform.

src/demo.re Outdated

let genPresentEnding ending subject => {
Js.log("subject", subject);
switch subject {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So one of the really cool things about Reason pattern matching is you can match on multiple things at the same time. E.g.,

switch (subject, ending) {
| (Yo, _) => "yo"
| (Tu, Ar) => "as"
| (Tu, _) => "es"
| (Ella, Ar) => "a"
| (Ella, _) => "e"
| (Nosotras, _) => "amos"
| (Vosotras, _) => "\195\161is" /* Reason source code is not Unicode, btw */
| (Ellas, _) => "an"
};

This keeps your matches really nice and flat.

@@ -0,0 +1,38 @@
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file should be inside the .vscode directory

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not using visual studio (pure vim), so therefore do not have .vscode directory. Any better folder to place this in, or should I have not committed this file?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, if you're not using Visual Studio Code, you don't need this. This is like a per-project .vimrc but for VSCode.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh wow. thanks!

};

let conjugate word info => {
let start = ( String.length word ) - 2;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function application 'binds more tightly' than operator application, so you can write String.length word - 2


let genFutureEnding ending subject => {
switch (subject:Subject.t, ending:Ending.t) {
| (Yo, _) => "\130"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the {js| ... |js} technique here as well for Unicode.

};

let genPresentEnding ending subject => {
switch (subject:Subject.t, ending:Ending.t) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't really need the type annotations, Reason will infer them 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants