技术控

    今日:59| 主题:49369
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Eclipse Golo JVM programming language

[复制链接]
九命猫 发表于 2016-11-28 16:32:11
26 0

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

Eclipse Golo JVM programming language-1 (especially,following,research,question,settings)

   Solar eclipse image via Shutterstock
  Yet another language? That is a good question to ask, especially when the Java Virtual Machine (JVM) is host to many languages with proven adoption. While Golo is indeed “yet another JVM language”, it has interesting applications for tinkerers as a standalone language, as an embedded language into other JVM applications, or even for quick prototyping in IoT settings.
      Eclipse Golo started out of the experiments from the Dynamid research team at CITI-Inria laboratory and INSA-Lyon . As we were investigating the possibilities offered by the new invokedynamic JVM bytecode instruction in Java 7, we ended up designing a simple programming language around that instruction and the corresponding java.lang.invoke API. We thought it would be interesting to pursue the development of Golo for the following reasons:
   
       
  • some other JVM languages port their semantics on top of invokedynamic , while we went the other way by designing the language around it with performance patterns in mind, and   
  • other proven JVM languages tend to have intimidating code bases, especially for students, and we needed something more approachable to experiment with language or runtime research.   
    While Golo is indeed “yet another JVM language” , it has interesting applications for tinkerers as a standalone language, as an embedded language into other JVM applications, or even for quick prototyping in IoT settings.
    Golo is now a Technology Project at the Eclipse Foundation, and a small friendly community is taking care of its evolution.
   Getting started with Golo

    Golo files usually use the .golo extension. Each Golo file defines a module, as in:
   [code]module EchoArgs

function main = |args| {
  println("Here are the command line arguments, if any:")
  foreach arg in args {
    println("-> " + arg)
  }
}[/code]    The main function of a module can serve as a program entry-point with command-line arguments being passed as an array. Golo programs can be compiled to JVM bytecode, or they can be executed directly. In each case the sole golo command-line tool provides a specific sub-command:
   [code]$ golo compile echo-args.golo
$ golo golo --files echo-args.golo --args 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$[/code]    The compile sub-command compiles to JVM bytecode, run executes some already-compiled Golo code, and golo serves as a shortcut to compile and directly run some code. There also exists a support for Unix-style “shebang” scripts:
   [code]module EchoArgs

function main = |args| {
  println("Here are the command line arguments, if any:")
  foreach arg in args {
    println("-> " + arg)
  }[/code]    By making the .golo file executable, we can directly run it:
   [code]$ chmod +x echo-args.golo
$ ./echo-args.golo 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$[/code]   Collection literals, anonymous functions and collection comprehensions

   Golo provides support for collection literals: tuples, lists, vectors, sets, and maps. It is possible to create a list as follows:
   [code]# Note: an instance of java.util.ArrayList
#
# This is equivalent to:
#   let numbers = java.util.ArrayList()
#   numbers: add(1)
#   (...)
let numbers = list[1, 2, 3, 4, 5][/code]    All collection literals are backed by java.util collections bar n -ary tuples that rely on a Golo-specific implementation. This provides great interoperability with Java libraries.
    It is possible to invoke methods on objects in Golo using : between a receiver object and a method. Collections have additional methods to support functional idioms, so the following code takes the numbers list above, generates a new list with items incremented by 10 using map , and then computes the sum of all elements using reduce :
   [code]let sum = numbers:
  map(|n| -> n + 10):
  reduce(0, |acc, next| -> acc + next)[/code]    We can also see how anonymous functions are being passed to the map and reduce methods. It is possible to assign a reference to a function, compose it with another function, and call it by name:
   [code]let f = (|n| -> n + 1): andThen(|n| -> n * 2)
println(f(3))   # prints 8[/code]    Finally, Golo provides collection comprehensions that are very similar to those from Python:
    Here nums is a set of tuples [i, j] where i is an even number between 1 and 100, and j is a character between a and z . This also introduced range notations, as in [1..100] .
   Defining types

   Golo does not support the definition of classes, but it supports structures, unions and dynamic object types.
   Dynamic objects

    The simplest type definition is DynamicObject :
   [code]let p1 = DynamicObject():
  define("name", "Someone"):
  define("email", "[email protected]"):
  define("prettyPrint", |this| -> println(this: name() + " <" + this: email() + ">"))

p1: prettyPrint()[/code]    The define reserved method allows creating attributes or methods, which can then be used by name. In many ways this construction is similar to the “expando” objects in Groovy. There also exists a fallback method definition that captures all invocations to any method that hasn’t been defined. This can be useful for designing flexible APIs, much like some idioms from the Ruby / Rails communities.
   Structures

   Structures can be created as follows:
   [code]struct Point = {x, y}[/code]   This defines a data structure for points, and it can be created and manipulated as follows:
   [code]var p2 = Point(1, 2)
println(p2)   # "struct Point{x=1, y=2}"

p2: x(4)
println(p2: x())

let x, y = p2
println(x)
println(y)[/code]    Note that Golo supports destructuring , so that x and y get the values from the structure object. This also works for collections, including map entries.
    This Point definition does not do much alone, but we can add methods using augmentations . Augmentations can add methods to any type, including existing Java classes. An augmentation applies to code from the same module, or to code from another module that imports its definition. Here is how we can add shift and dump methods to Point objects:
   [code]$ golo compile echo-args.golo
$ golo golo --files echo-args.golo --args 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$0[/code]   With this augmentation visible we can then use the methods:
   [code]$ golo compile echo-args.golo
$ golo golo --files echo-args.golo --args 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$1[/code]   Enumerations

    Enumerations provide an elegant way to define custom types as tagged unions , sometimes also referred to as sum algebraic data types . Here is how one could define binary trees using an empty type, a leaf type and a node type:
   [code]$ golo compile echo-args.golo
$ golo golo --files echo-args.golo --args 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$2[/code]   With this definition, we can create both a tree and an empty tree:
   [code]$ golo compile echo-args.golo
$ golo golo --files echo-args.golo --args 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$3[/code]   Union types get query methods to check for their types, so that:
   [code]$ golo compile echo-args.golo
$ golo golo --files echo-args.golo --args 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$4[/code]    prints true for each test, since t0 is an empty tree and t1 is a leaf. It is also possible to check not just for the type but also for values:
   [code]$ golo compile echo-args.golo
$ golo golo --files echo-args.golo --args 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$5[/code]    Unknown.get() returns a placeholder value for union types, which allows us to check that t1 is a node whose left value is a leaf of value 1, while the right value is being ignored.
   Decorators

    Golo also supports Python-style function decorators . A decorator is basically a higher-order function, that is, a function that returns a function. Let’s get more practical, and let’s define the trace function below:
   [code]$ golo compile echo-args.golo
$ golo golo --files echo-args.golo --args 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$6[/code]    That function takes a message, and returns a function that decorates another function and captures its arguments ( args... is for variable length arguments, so it can capture any function arity). We can then decorate a function, as in:
   [code]$ golo compile echo-args.golo
$ golo golo --files echo-args.golo --args 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$7[/code]    When we call add(1, 2) , we get the following console output:
   [code]$ golo compile echo-args.golo
$ golo golo --files echo-args.golo --args 1 2 3 4 5
Here are the command line arguments, if any:
-> 1
-> 2
-> 3
-> 4
-> 5
$8[/code]   That was only a brief overview!

   This article did not highlight all the features of Eclipse Golo, and we encourage you to give it a try and discuss with the community.
   We would like to make a special call to other Eclipse projects: ironically Golo does not have a proper Eclipse IDE support yet. The best support remains the Atom or Vim editors. You will be more than welcome if you feel like helping us in bringing first-class support for Eclipse Golo in the Eclipse IDE!

Eclipse Golo JVM programming language-2 (especially,following,research,question,settings)

      This post was originally published in the  October  2016 issue of the Eclipse Newsletter: Discover Eclipse Runtimes    
    For more information and articles check out the Eclipse Newsletter .
友荐云推荐




上一篇:“黑客老王”联手“乌云一哥”重出江湖,为黑客“造梦”的幻云有多牛? ...
下一篇:Xamarin.Forms: Java.Lang.IllegalStateException
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表