v0.8 of Gleam, the statically typed language for the Erlang VM, is out

栏目: IT技术 · 发布时间: 6年前

内容简介:It’s been a really good couple months for Gleam. The community has been growing, we’ve been getting new features and improvements, and now it’s time for another release. Let’s take a look!Documentation has been a bit of sore point up until now. If you want

It’s been a really good couple months for Gleam. The community has been growing, we’ve been getting new features and improvements, and now it’s time for another release. Let’s take a look!

HTML Documentation

Documentation has been a bit of sore point up until now. If you wanted to write a program in Gleam there was no other way to see what types functions were available to use. More than likely you would have to have the source code for the standard library open in a window somewhere, which isn’t the nicest way to learn a language.

To solve this problem Gleam can now generate HTML documentation from your Gleam source code and publish to HexDocs, the home of documentation in the Erlang ecosystem.

Check out an example of the new documentation here .

Gleam format

Consistency matters in code! Having a consistent style makes code easier to read and easier to work with. On the other hand, maintaining a consistent style by hand is an expensive time sink, and it’s all too easy to waste time debating exactly where is the right place to put those curly braces in code reviews.

The solution to these problems is a code formatter. Write whatever code you want without spending any time on style, press save, and have your editor automatically fix the style before you’ve taken your fingers off the keyboard.

Starting with this release Gleam includes a formatter, and all new Gleam projects are preconfigured to have their format validated on CI. Enable the formatter in your editor and never waste time on fixing style again.

If you’re interested in learning more about formatters or how they work, a few years back I gave a talk on the subject. It is in the context of the Elixir language, but it all holds true to Gleam as well.

Fancy pipes

The pipe operator is a favourite feature of Gleam, it allows us to write chains of functions that read top-to-bottom and left-to-right.

It works by taking the value on the left hand side and using it as the argument to the function on the right hand side.

// Without pipe
print(eval(read(stdin)), stdout)

// With pipe
stdin |> read |> eval |> print(_, stdout)

In the example above the function “print” takes two arguments, so the function _ capture syntax has been used to tell Gleam where to insert the left hand side value. In practice most Gleam functions take multiple arguments, so the capture syntax is needed more often than not.

scores
|> list.map(_, double)
|> list.fold(_, 0, add)
|> should.equal(_, 42)

With Gleam v0.8’s fancy pipe this boilerplate is no longer needed!

When used with a function call on the right hand side the pipe will first check to see if the left hand value could be used as the first argument to the call, and if not it falls back on using it as the argument to the call.

scores
|> list.map(double)
|> list.fold(0, add)
|> should.equal(42)

Much better! And what’s more this is resolved at compile time using the type system so there is no performance or safety penalty to use this over nested function calls.

Accessor syntax

Pattern matching is great, but sometimes we just want to get a single value out of a record or tuple. To enable this Gleam now supports the .field syntax that’s common in other languages.

It works with records:

let student = Student(subject: "Theatre")
let mary = Person(name: "Mary", role: student)

mary.name         // => "Mary"
mary.role.subject // => "Theatre"

And it works with tuples:

let pair = tuple("Apples", "Oranges")

pair.0 // => "Apples"
pair.1 // => "Oranges"

Spread operator

Gleam’s pattern matching has been expanded with a new operator called the spread operator, which allows you to write a partial pattern for a record.

// Without spread
let Uri(
  scheme: scheme,
  port: port,
  path: _,
  query: _,
  fragment: _,
  authority: _,
  userinfo: _,
  host: _,
) = uri

// With spread
let Uri(scheme: scheme, port: port, ..) = uri

To be consistent with records the syntax for prepending to a list and for matching on the head of a list have been updated to also use the spread operator.

let new_list = [1, 2, 3, ..existing_list]

case list {
  [first, ..] -> first
  [] -> 0
}

The old [x | xs] list syntax is deprecated but is still accepted. If you run the formatter it will automatically upgrade your code for you, so do that first!

The rest

In addition to these changes there’s also support for numbers and numerical operators in case guards, an “assert” syntax for inexhaustive pattern matching, a “todo” syntax for type checking functions without a finished implementation, a handful of performance improvements, and a whole bunch of bug fixes!

Outside of the compiler we continue to grow the standard library, and we have made good progress in OTP support with a prototype for type safe supervision trees.

For more information check out the changelog on Github.

Thanks

The most exciting thing for me about this release is just how much the community has grown since the last release. We’ve more people writing Gleam, more people sharing ideas, and more people working on the compiler. We’ve even got the beginnings of a Gleam web server and web framework in the Midas project!

It would not have been possible to have a release this big without the support of all Gleam’s sponsors and contributors, so a huge thank you to them:

If you would like to help make strongly typed programming on the Erlang virtual machine a production-ready reality please consider sponsoring Gleam via the GitHub Sponsors program.

Thanks for reading! Have fun! :purple_heart:


以上所述就是小编给大家介绍的《v0.8 of Gleam, the statically typed language for the Erlang VM, is out》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

AI极简经济学

AI极简经济学

阿杰伊·阿格拉沃尔、乔舒亚·甘斯、阿维·戈德法布 / 闾佳 / 湖南科技出版社 / 2018-12-1 / 58.00

人工智能正在以不可阻挡的态势席卷全球。无论是iPhone的神经网络引擎、AlphaGo的围棋算法,还是无人驾驶、深度学习……毫无疑问,人工智能正在改写行业形态。如同此前个人电脑、互联网、大数据的风行一般,技术创新又一次极大地改变了我们的工作与生活。 那么,究竟应该如何看待人工智能?在《AI极简经济学》一书中,三位深耕人工智能和决策领域的经济学家给出了清晰的答案。他们以坚实的经济学理论剖析动态,把握......一起来看看 《AI极简经济学》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试