wende / elchemy Goto Github PK
View Code? Open in Web Editor NEWWrite Elixir code using statically-typed Elm-like syntax (compatible with Elm tooling)
Home Page: https://wende.github.io/elchemy/
License: MIT License
Write Elixir code using statically-typed Elm-like syntax (compatible with Elm tooling)
Home Page: https://wende.github.io/elchemy/
License: MIT License
a b =
case lol b of
_ -> 2
curry a/1
def a(_) do
2
end
but it should
curry a/1
def a(b) do
lol().(b)
end
Example
meta = [
"use GenServer",
"defstruct [:something, :else]
]
->
use Genserver
defstruct [:field1, :field2, field3: "default")
http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Maybe
import Maybe exposing ( Maybe( Just, Nothing ) )
Add:
{-| my function that is awesome -}
function : Awesome
function = Awesome
->
@doc "my function that is awesome"
@spec function :: :awesome
def function(), do: :awesome
This is an AST job because elm-ast sees no difference between block and regular comments
Change defcurry to something nicer like for example:
a b c d = 10
->
curry a/3
def a(b, c, d), do: 10
Or automatically generate curried functions in @before_compile macro
Add:
first (fst, snd) =
fst
->
def first({fst, snd}) do
fst
end
That would make testing with Elmchemy much easier
{-| Extract the first element of a list.
head [1,2,3] == Just 1
head [] == Nothing
-}
head : List a -> Maybe a
head list =
case list of
x :: xs ->
Just x
[] ->
Nothing
->
@doc """
Extract the first element of a list.
iex> head.([1,2,3])
1
iex> head.([])
nil
"""
@spec head : List a -> Maybe a
def head([x | xs]) do
x
end
def head([]) do
nil
end
Add:
a fn = fn
Right now specs behave incorrectly when we define a function without taking all of the parameters
someFun : Int -> String
someFun =
to_string
->
@spec some_fun(integer) :: String.t
def some_fun() do
to_string
end
While it's perfectly fine syntax used very often to make default arguments
http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Result
import Result exposing ( Result( Ok, Err ) )
From https://github.com/elm-lang/core#default-imports :
a >> b >> c
->
fn arg -> arg |> a |> b |> c end
Add:
fun : (a -> b) -> (a -> b)
->
@spec fun(any -> any) :: (any -> any)
instead of
@spec fun(any) :: anyany) :: any
elixirE expression context
->
elixirE context expression
Example
len li =
case li of
[] -> 0
a :: rest -> 1 + len rest
->
def len([]), do: 0
def len([a | rest]), do: 1 + len(rest)
Add:
Let
let
a = 1
in
a * 2
->
a = 1
a * 2
Add:
case t of
-- Some comment regarding our case
OurCase c -> 1
->
case t do
# Some comment regarding our case
{:our_case, c} -> 1
Example
a = 2
->
@a 2
How to process messages is still up to a debate:
Temporary solution:
case receive of
Msg a -> 1
->
receive do
{:msg, a} -> 1
But since it's impure, unidiomatic, and totally forbids a case of after
block it should be discussed and redeveloped after
Add:
case a of
Type _ as m -> 1
->
case a do
{:type, _} = m -> 1
Add:
type State a = List a | Map a
fun : State a
->
@spec fun(list(a) | %{})
Add: Simple structs (more than one field is not yet support in ast parser)
point = {x = 1}
{ point | x = 2 }
->
point = %{x => 1}
%{ point | x => 2 }
Add:
type alias MyType a = List a
MyType Int
->
list(int)
instead of
list(any)
type alias MyType a b = (List a, Maybe b)
MyType Int String
->
{list(int), {String.t} | nil}
instead of
{list(any), any | nil}
Requires major change in AST
Bogdanp/elm-ast/#5
Add:
GT
->
:gt
instead of
:g_t
Add:
[(1,1), (1,2)]
->
[{1,1}, {1, 2}]
instead of
[1,1, {1, 2}]
The cause of that is that Lists and Tuples are implemented on binary operator ,
instead of being parsed as an entity
Add:
()
->
{}
So, there appears to be a slight typing failure. :-)
module Stack exposing (..)
type alias TestRecord =
{ int : int
, float : float
}
blah : TestRecord
blah =
42
blorp =
blah.x
->
# Compiled using Elmchemy v0.0.17
defmodule Stack do
use Elmchemy
@spec blah :: %{int: any, float: any}
curry blah/0
def blah() do
42
end
curry blorp/0
def blorp() do
blah.x
end
end
As an example, my MLElixir project (which puts a strongly-typed ML face on Elixir, it is not a released project, just something I screw around with for fun, its errors are also very low level and not prettied up at all) compiles the similar code as the above Elm:
iex> import MLElixir
MLElixir
iex> defmlmodule Stack do
...> type test_record = %{
...> int: integer,
...> float: float,
...> }
...>
...> let blah | test_record = 42
...>
...> let blorp = blah.x
...> end
Gives this:
** (throw) {:NO_TYPE_RESOLUTION, %TypedElixir.Type.Const{const: :integer, meta: %{values: '*'}}, %TypedElixir.Type.Const{const: :integer, meta: %{values: '*'}}, %TypedElixir.Type.Record{labels: [int: %TypedElixir.Type.Const{const: :integer, meta: %{}}, float: %TypedElixir.Type.Const{const: :float, meta: %{}}], meta: %{}}}
Which is entirely expected, but currently the above elm code is working in elmchamy when it absolutely should not.
Looking forward to this being developed out. :-)
Example
function
a
b
c
It's a very tricky one.
Since Elm recognizes next argument or case branch based on indentation
For that we need a major change in the parser
Bogdanp/elm-ast#1
Add:
mod "Some.Module"
->
Some.Module
Add:
(~~) a b = a ++ b
a ~~ b
->
curry _dashdash/2
def _dashdash(a, b) do
a + b
end
_dashdash(a, b)
It requires to look through the types and type aliases of these files though, or otherwise they won't alias properly
Add:
sortBy .name [chuck,alice,bob]
->
sortBy.(&Map.fetch!(&1, :name)).([chuck,alice,bob])
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.