Scala笔记(5)——apply method

本文选自Deeper Look at the Apply Method in Scala

In Scala, there is a language feature generally referred to as “The Apply Method” that has the following rules:

Any object that has an apply method can be called with the .apply omitted.
Functions are no more than objects.

Let’s take a look at an example. Given the following abbreviated definition of class Array, and an instance a,

class Array{
def apply(index:Int) = { …some code to get from the array… }
}

val a = new Array(whatever)

Then the following calls are essentially equivalent:

a.apply(7)
a(7)

Scala语言的特性

Scala语言有以下几个特性:

(1)Scala代码运行在JVM上,因此可以利用Java语言的丰富资源。
(2)Scala语言是一门“纯面向对象的编程语言(pure object-oriented programming(OOP) language)”:每个变量都是对象,每个运算符都是方法;
(3)Scala语言也是一门函数式编程语言(functional programming(FP) language),因此可以把函数赋给变量。

*NIX & Hacking —— 第3期

做一本我感兴趣的杂志,就这么简单!

D

Lessons Learned: Writing a filesystem in D

Golang

An Exercise in Profiling a Go Program
Let’s learn Go!

Javascript

jsbeeb

Kernel

Build a Red Hat 7 custom kernel
How to Compile Linux Kernel from Source to Build Custom Kernel

Scala

Difference between object and class in Scala

Security

Introducing s2n, a New Open Source TLS Implementation

Unix

Solaris new system calls: getentropy(2) and getrandom(2)
websocketd
When ‘int’ is the new ‘short’

Virtualization

Understanding VT-d: Intel Virtualization Technology for Directed I/O

Easter egg

FUDCON PUNE 2015
Multiple choice
What is the difference between x64 and IA-64?

Scala笔记(2)——val和var

Scala中,用val声明一个变量是不可改变的(immutable,read-only),而用var声明一个变量是可改变的(immutable,read-write)。同Java类似,Scala中许多变量实质上对分配在堆(heap)上的对象(object)的引用(reference),所以可变或不可变指的是引用可不可以指向不同的对象,而并非指引用的对象本身是否可以改变。看下面这个例子:

scala> val array: Array[String] = new Array(5)
array: Array[String] = Array(null, null, null, null, null)

scala> array[0] = "Hello"
<console>:1: error: identifier expected but integer literal found.
       array[0] = "Hello"
             ^

scala> array = new Array(2)
<console>:8: error: reassignment to val
       array = new Array(2)
             ^

array是一个val类型变量,array所指向数组的第一个元素可以改变(array[0] = "Hello"),但是array不能指向一个新的数组(array = new Array(2))。

Scala笔记(1)——一个入门的Scala程序

这是一个简单的入门Scala程序(iterate.scala):

object Iterate {
  def main (args: Array[String]) {
    args.foreach(s => println(s))
  }
}

在程序中,Iterate被定义成objectScalaobjectsingleton,也就是运行时只能产生一个Iterate实例。

main方法只能定义在object中,传入到main方法中的命令行参数是一个字符串数组:args: Array[String]main方法可以看成是Java类的static main方法,即Iterate程序的入口函数。

s => println(s)是一个functional literal,做的仅仅是打印s而已。

执行这个程序:

[root@Fedora scala]# scala iterate.scala  1 2
1
2

可以看出打印了命令行参数。

*NIX & Hacking —— 第2期

做一本我感兴趣的杂志,就这么简单!

Debug

Introduction to Debuggers

Docker

DOCKER MACHINE 0.3.0 DEEP DIVE

Git

First aid git

Golang

go-lang-cheat-sheet

Kernel

Kernel and userspace tracing: with LTTng and his friends

Regular Expression

Five Invaluable Techniques to Improve Regex Performance

Rust

First Rust Program Pain (So you can avoid it…)

Scala

Various ways to run Scala code
Why does the same scala code work OK in command line while not in Intellij?

Unix

A little collection of cool unix terminal/console/curses tools
How big is the pipe buffer?
Stack Smashing On A Modern Linux System
Unix history repository

Easter egg

DMA (Direct Memory Access)
Get Unix Jobs
Here are 13 tech jobs that pay at least $130,000 across the country

如何运行scala脚本

搭建Scala开发环境一文中可以看到不加任何参数直接运行scala命令时,会启动REPL(Read,Eval,Print,Loop)环境,然后就可以在这个环境里交互地执行Scala代码。本文介绍如何运行Scala脚本(script)。

首先创建一个简单的Scala脚本(hello.scala):

print("Hello world!\n")

a)直接运行scala hello.scala

[root@Fedora scala]# scala hello.scala
Hello world!

b)在REPL环境用load命令运行hello.scala

[root@Fedora scala]# scala
Welcome to Scala version 2.10.4 (OpenJDK 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :load hello.scala
Loading hello.scala...
Hello world!

c)如果想把脚本编译成JVM byte code (一组*.class文件),可以使用scalac程序(命令行选项是-Xscript <object><object>是“main class”的名字,也即Java程序的入口):

[root@Fedora scala]# scalac -Xscript hello hello.scala
[root@Fedora scala]# ls
hello$$anon$1.class  hello.class  hello$.class  hello.scala
[root@Fedora scala]# scala hello
Hello world!

也可以用scalap这个逆向分析工具分析一下hello.class

[root@Fedora scala]# scalap -cp . hello
object hello extends scala.AnyRef {
  def this() = { /* compiled code */ }
  def main(argv : scala.Array[scala.Predef.String]) : scala.Unit = { /* compiled code */ }
}