Scala in a Nutshell

Ubuntu上使用Mill的Chisel工程入门 Ubuntu上使用Mill的Chisel工程入门开始NutShell项目介绍删除原来的代码书写我们的代码书写执行翻译的Scala代码运行整个工程安装Mill使用Mill运行整个工程 开始 还记得劳资刚刚开始学chisel的时候,跟着github上的Bootcamp学习了一大堆chisel语法(然而不知道这些语法怎么用2333) 彼时彼刻,困扰老子的问题莫过于怎么在自己电脑的ubuntu上(其实是虚拟机辣)把那些chisel代码转化为verilog。 后来啊,老子突然就明白了,老子只要把开源的chisel项目 阅读详情

Scala in a Nutshell

Intro

Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It seamlessly integrates features of object-oriented and functional languages.

object-oriented functional statically typed extensible interoperates

Basics

@main def hello(): Unit = println("hello Scala!")

Variables

val a: Int = 2
val b: String = "hello"

var c: Int = 3
  • val - value variable works like final in Java, value cannot be changed after initialization
  • var - vary variable, value can be changed after definition/initialization

Expressions

Block

Control Structures

Unified Types

Intro

在这里插入图片描述

Scala is a unique language in that it’s statically typed, but often feels flexible and dynamic. For instance, thanks to type inference you can write code like this without explicitly specifying the variable types:

val a = 1
val b = 2.0
val c = "Hi!"

Union types in Scala 3

def isTruthy(a: Boolean | Int | String): Boolean = ???
def dogCatOrWhatever(): Dog | Plant | Car | Sun = ???

Statically-typed programming languages offer a number of benefits, including:

  • Helping to provide strong IDE support
  • Eliminating many classes of potential errors at compile time
  • Assisting in refactoring
  • Providing strong documentation that cannot be outdated since it is type checked

Inferred Types

val x: Int = 1
val y: Double = 1

val a = 1
val b = List(1, 2, 3)
val m = Map(1 -> "one", 2 -> "two")

// don't need to declare the type when defining value binders
scala> val a = 1
val a: Int = 1

scala> val b = List(1, 2, 3)
val b: List[Int] = List(1, 2, 3)

scala> val m = Map(1 -> "one", 2 -> "two")
val m: Map[Int, String] = Map(1 -> one, 2 -> two)

Generics

// here we declare the type parameter A
//          v
class Stack[A]:
  private var elements: List[A] = Nil
  //                         ^
  //  Here we refer to the type parameter
  //          v
  def push(x: A): Unit =
    elements = elements.prepended(x)
  def peek: A = elements.head
  def pop(): A =
    val currentTop = peek
    elements = elements.tail
    currentTop

Intersection Types

Used on types, the & operator creates a so called intersection type. The type A & B represents values that are both of the type A and of the type B at the same time.

trait Resettable:
  def reset(): Unit

trait Growable[A]:
  def add(a: A): Unit

def f(x: Resettable & Growable[String]): Unit =
  x.reset()
  x.add("first")

x is a subtype of both Resettable and Growable.

& is commutative: A & B is the same type as B & A.

Union Types

Used on types, the | operator creates a so-called union type. The type A | B represents values that are either of the type A or of the type B.

case class Username(name: String)
case class Password(hash: Hash)

def help(id: Username | Password) =
  val user = id match
    case Username(name) => lookupName(name)
    case Password(hash) => lookupPassword(hash)
  // more code here ...
Inference of Union Types

The compiler assigns a union type to an expression only if such a type is explicitly given.

val name = Username("Eve")     // name: Username = Username(Eve)
val password = Password(123)   // password: Password = Password(123)

scala> val a = if true then name else password
val a: Object = Username(Eve)

scala> val b: Password | Username = if true then name else password
val b: Password | Username = Username(Eve)

Union types are duals of intersection types. And like & with intersection types, | is also commutative: A | B is the same type as B | A.

Variance

trait Item { def productNumber: String }
trait Buyable extends Item { def price: Int }
trait Book extends Buyable { def isbn: String }

// an example of an invariant type
trait Pipeline[T]:
  def process(t: T): T

// an example of a covariant type
trait Producer[+T]:
  def make: T

// an example of a contravariant type
trait Consumer[-T]:
  def take(t: T): Unit

In general there are three modes of variance:

  • invariant—the default, written like Pipeline[T]
  • covariant—annotated with a +, such as Producer[+T], like<? extends T>in Java
  • contravariant—annotated with a -, like in Consumer[-T], like <? super T>in Java

When?

  • Producers are typically covariant, and mark their type parameter with +. This also holds for immutable collections (List, Vector).
  • Consumers are typically contravariant, and mark their type parameter with -.
  • Types that are both producers and consumers have to be invariant, and do not require any marking on their type parameter. Mutable collections like Array fall into this category.

Algebraic Data Types

Opaque Types

Structural Types

class Record(elems: (String, Any)*) extends Selectable:
  private val fields = elems.toMap
  def selectDynamic(name: String): Any = fields(name)

type Person = Record {
  val name: String
  val age: Int
}

val person = Record(
  "name" -> "Emma",
  "age" -> 42
).asInstanceOf[Person]

println(s"${person.name} is ${person.age} years old.")

The parent type Record in this example is a generic class that can represent arbitrary records in its elems argument. This argument is a sequence of pairs of labels of type String and values of type Any. When you create a Person as a Record you have to assert with a typecast that the record defines the right fields of the right types. Record itself is too weakly typed, so the compiler cannot know this without help from the user. In practice, the connection between a structural type and its underlying generic representation would most likely be done by a database layer, and therefore would not be a concern of the end user.

Record extends the marker trait scala.Selectable and defines a method selectDynamic, which maps a field name to its value. Selecting a structural type member is done by calling this method. The person.name and person.age selections are translated by the Scala compiler to:

person.selectDynamic("name").asInstanceOf[String]
person.selectDynamic("age").asInstanceOf[Int]

Besides selectDynamic, a Selectable class sometimes also defines a method applyDynamic. This can then be used to translate function calls of structural members. So, if a is an instance of Selectable, a structural call like a.f(b, c) translates to:

a.applyDynamic("f")(b, c)

Dependent Function Types

Other Types

  • Type lambdas
  • Match types
  • Existential types
  • Higher-kinded types
  • Singleton types
  • Refinement types
  • Kind polymorphism

Functions

HOF (High Order Functions)

a function that (a) takes other functions as input parameters or (b) returns a function as a result.

HOF are possible because functions are first-class values.

Methods

implicit (v2)

Extends existing class without inherits it. It is replaced by extension methods in Scala 3.

LogicalPlan.scala

class LogicalPlan (val name: String) {
}

ParserUtils.scala

object ParserUtils {
  implicit class EnhancedLogicalPlan(val plan: LogicalPlan) extends AnyVal {
    def optional(ctx: AnyRef)(f: => LogicalPlan): LogicalPlan = {
      if (ctx != null) {
        println(s"$ctx: ${plan.name}")
        f
      } else {
        plan
      }
    }
  }
}

test.scala

import ParserUtils._

object Hello {
    def main(args: Array[String]): Unit = {
        val plan = new LogicalPlan("logical plan")
    	plan.optional("hello") {
      		print("scala")
      		plan
    	}
    }
}

extension methods in Scala 3

case class Circle(x: Double, y: Double, radius: Double) {}

extension (c: Circle)
  def circumference: Double = c.radius * math.Pi * 2

object ExtensionMethodsTest {
  def main(args: Array[String]): Unit = {
    val c = new Circle(1.0, 2.0, 5.5)
    println(c.circumference)
  }
}

currying

def left[A, B](as: Seq[A], init: B)(op: (B, A) => B) = {
  var ans = init
  as.foreach(item => {
    ans = op(ans, item)
  })
  ans
}

@main def run() = {
  println("Hello, World!")
  println(left(Seq(1, 2, 3), 0)(_+_))
}
  override def parsePlan(sqlText: String): LogicalPlan = parse(sqlText) { parser =>
    astBuilder.visitSingleStatement(parser.singleStatement()) match {
      case plan: LogicalPlan => plan
      case _ =>
        val position = Origin(None, None)
        throw new ParseException(Option(sqlText), "Unsupported SQL statement", position, position)
    }
  }
  protected def parse[T](command: String)(toResult: SqlBaseParser => T): T = {
    logDebug(s"Parsing command: $command")

    val lexer = new SqlBaseLexer(new UpperCaseCharStream(CharStreams.fromString(command)))
    lexer.removeErrorListeners()
    lexer.addErrorListener(ParseErrorListener)

    val tokenStream = new CommonTokenStream(lexer)
    val parser = new SqlBaseParser(tokenStream)
    parser.addParseListener(PostProcessor)
    parser.addParseListener(UnclosedCommentProcessor(command, tokenStream))
    parser.removeErrorListeners()
    parser.addErrorListener(ParseErrorListener)
    parser.legacy_setops_precedence_enbled = conf.setOpsPrecedenceEnforced
    parser.legacy_exponent_literal_as_decimal_enabled = conf.exponentLiteralAsDecimalEnabled
    parser.SQL_standard_keyword_behavior = conf.ansiEnabled

    try {
      try {
        // first, try parsing with potentially faster SLL mode
        parser.getInterpreter.setPredictionMode(PredictionMode.SLL)
        toResult(parser)
      }
      catch {
        case e: ParseCancellationException =>
          // if we fail, parse with LL mode
          tokenStream.seek(0) // rewind input stream
          parser.reset()

          // Try Again.
          parser.getInterpreter.setPredictionMode(PredictionMode.LL)
          toResult(parser)
      }
    }
    catch {
      case e: ParseException if e.command.isDefined =>
        throw e
      case e: ParseException =>
        throw e.withCommand(command)
      case e: AnalysisException =>
        val position = Origin(e.line, e.startPosition)
        throw new ParseException(Option(command), e.message, position, position)
    }
  }

Traits

Classes

Case Classes

Singleton Objects

Collections

  • Sequences
  • Maps
  • Sets
    在这里插入图片描述

Immutable

在这里插入图片描述

Mutable

在这里插入图片描述

Functional Programming

Definition

The essence of Scala is a fusion of functional and object-oriented programming in a typed settings:

  • Functions for logic
  • Objects for the modularity
import spark.implicits._
import java.io.File
new File("/data/projects/tpcds/data").listFiles.filter(_.isDirectory).map(_.listFiles.filter(_.isFile)).flatten.foreach(f => spark.read.options(Map("delimiter" -> "|")).csv(f"file://${f.getCanonicalPath}").write.options(Map("compression"->"SNAPPY")).parquet(f"file:///data/projects/tpcds/parquet/${f.getParentFile.getName}"))

Definition for Wikipedia

Functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that each return a value, rather than a sequence of imperative statements which change the state of the program.

In functional programming, functions are treated as first-class citizens, meaning that they can be bound to names (including local identifiers), passed as arguments, and returned from other functions, just as any other data type can. This allows programs to be written in a declarative and composable style, where small functions are combined in a modular manner.

It can also be helpful to know that experienced functional programmers have a strong desire to see their code as math, that combining pure functions together is like combining a series of algebraic equations.

The feeling that you’re writing math-like equations (expressions) is the driving desire that leads you to use only pure functions and immutable values, because that’s what you use in algebra and other forms of math.

immutable values

use List, Vector, Map, and Set. Use case class, whose constructor parameters are val by default.

pure functions

A pure function can be defined as:

  • A function f is pure if, given the same input x, it always returns the same output f(x)
  • The function’s output depends only on its input variables and it’s implementation
  • It only computes the output and does not modify the world around it (no side effects)

This implies:

  • It doesn’t modify its input parameters
  • It doesn’t mutate any hidden state
  • It doesn’t have any “back doors”: It doesn’t read data from the outside world (including the console, web services , databases, files, etc.), or write data to the outside world

Of course an application isn’t very useful if it can’t read or write to the outside world, so people make this recommendation:

Write the core of your application using pure functions, and then write an impure “wrapper” around that core to interact with the outside world.

Error handling

Use Option, Some and None

def makeInt(s: String): Option[Int] = 
	try
		Some(Integer.parseInt(s.trim))
     catch
		case e: Exception => None
Consume
  • match

    makeInt(x) match
      case Some(i) => println(i)
      case None => println("That didn’t work.")
    
  • for

    val y = for
      a <- makeInt(stringA)
      b <- makeInt(stringB)
      c <- makeInt(stringC)
    yield
      a + b + c
    

    if any of the 3 strings can’t be converted to an Int, y will be None

Using Option to replace null
class Address(
  var street1: String,
  var street2: Option[String],   // an optional value
  var city: String, 
  var state: String, 
  var zip: String
)
Alternatives

For example, a trio of classes known as Try/Success/Failure work in the same manner, but (a) you primarily use these classes when your code can throw exceptions, and (b) you want to use the Failure class because it gives you access to the exception message. For example, these Try classes are commonly used when writing methods that interact with files, databases, and internet services, as those functions can easily throw exceptions.

Concurrency

When you want to write parallel and concurrent applications in Scala, you can use the native Java Thread—but the Scala Future offers a more high level and idiomatic approach, so it’s preferred.

A Future represents a value which may or may not currently be available, but will be available at some point, or an exception if that value could not be made available.

【数据分析生命周期全揭秘】从零开始打造你的数据科学项目 | 最全指南 在当今数据驱动的世界中,数据科学正在颠覆传统行业,成为企业决策和价值创造的重要引擎。然而,如何科学、系统地开展一个数据分析项目?这是很多人关心的核心问题。本篇文章基于经典数据分析课程内容,深入解读数据分析生命周期的每个阶段,让你快速掌握从“发现问题”到“落地应用”的完整流程。如果你正在学习或准备从事数据分析领域,这篇文章一定会对你大有帮助!数据分析生命周期的六大阶段数据分析生命周期的六个阶段相辅相成,每个阶段都至关重要:发现问题:明确需求,提出假设。数据准备:清洗、转换和可视化数据。 阅读详情

相关推荐

从JTAG到AS模式:一文搞懂EP4CE10E22C8N所有配置引脚(MSEL, nCONFIG, DATA0)的用法

本文深入解析EP4CE10E22C8N FPGA的配置引脚(MSEL, nCONFIG, DATA0)在不同模式下的使用方法,特别关注从JTAG到AS模式的切换要点。通过详细的配置模式对比、状态机分析和PCB布局建议,帮助开发者避免常见陷阱,提升FPGA配置成功率。

weixin_33713350的博客 322

NutShell 项目使用教程

NutShell 项目的目录结构如下: ``` NutShell/ ├── fpga/ │ ├── board/ │ ├── boot/ │ ├── lib/ │ ├── Makefile │ ├── Makefile.check │ └── NutShell.tcl ├── src/ ├── tools/ ├── .gitignore ├── .gitmodules ├

gitblog_00039的博客 1172

基于ENVI5.6与SARscape的哨兵一号时序InSAR实战:从数据准备到PS/SBAS结果解译

本文详细介绍了基于ENVI5.6与SARscape软件,利用哨兵一号(Sentinel-1)卫星数据,进行PS-InSAR与SBAS-InSAR时序分析,以监测地表形变的完整实战流程。内容涵盖从数据下载、软件配置、预处理、核心处理步骤到结果解译与质量评估,重点解析了PS与SBAS方法的关键参数设置、常见问题及解决方案,旨在帮助用户掌握从数据准备到成果分析的完整技术链条。

oo7890的博客 636

NutShell 开源项目常见问题解决方案

NutShell 是一个由 OSCPU(Open Source Chip Project by University)团队开发的处理器项目。该项目支持 RISC-V 架构,包括 riscv64 和 riscv32 指令集。NutShell 项目的主要编程语言是 Scala,使用 Chisel 硬件描述语言进行硬件设计。 ## 新手使用注意事项及解决方案 ### 1. 环境配置问题 **问题描

gitblog_00548的博客 894

3DE重客户端安装

3DE重客户端安装

m0_48170265的博客 9440

Windows系统下com0com虚拟串口安装与驱动签名禁用全攻略

本文详细介绍了在Windows系统下安装com0com虚拟串口工具的全过程,包括驱动签名问题的解决方法。通过禁用驱动程序强制签名,用户可以顺利安装并使用这款开源工具创建虚拟串口对,适用于软件开发测试、硬件调试等场景。文章还提供了验证虚拟串口功能的实用技巧和常见问题解决方案。

weixin_28720573的博客 390

Learning Java characteristics (Java in a Nutshell 6th)

Java characteristics: Java .class files are machine-independent, including the endianness. Java .class files are not optimized .class files comprises "bytecode" different from "interpreted langu...

weixin_34319817的博客 178

现代SerDes解剖:从CTLE/DFE到PAM4与ADC+DSP架构

高速有线通信的物理层瓶颈,正从并行总线的时钟偏斜转向串行链路的信号完整性挑战。SerDes作为解决这一问题的核心技术,其内部架构远超“并转串”的朴素认知。接收端的CTLE线性均衡与DFE判决反馈均衡,配合CDR时钟恢复,共同构成对抗信道损耗与码间干扰的关键机制。随着速率迈向56G/112G,PAM4调制以电平幅度换取带宽,而ADC+DSP架构则把均衡算法搬入数字域,实现更灵活的非线性补偿。理解这些模块如何协同工作,是调试PCIe、以太网等高速链路、优化眼图余量与抖动预算的基础,也是硬件工程师从盲目调参走向系

weixin_29194817的博客 301

如何使用spinalHDL/chisel帮助我们灵活的开发

NutShell 果壳如何利用chisel开发。

weixin_44020424的博客 832

【AI大模型应用开发实战】大型语言模型评估指南白皮书(LLM Eval)

近年来,大型语言模型(LLM)在自然语言处理领域取得了重大进展,例如 GPT-3 和 Chat-GPT。这些模型经过大型数据集的训练,在文本相关任务中表现出卓越的能力,甚至超越了人类。本文将简要介绍如何验证 LLMs 性能的评估指标。自然语言处理(NLP)是人工智能的一个领域,涉及计算机和人类语言之间的交互。NLP 的基本任务之一是语言建模 (LM),它涉及构建统计模型来分析和生成自然语言。LM 已成为推进机器语言智能的关键方法,使机器能够执行机器翻译、情感分析和对话系统等任务。

AI天才研究院 1017

【K8S系列】在 Kubernetes 中使用 Prometheus 进行监控的详细指南

Prometheus 是一个开源的监控和报警工具,广泛用于 Kubernetes 环境中。本文将深入探讨如何通过 Kubernetes 中的注解配置 Prometheus 的抓取设置,以高效监控服务,并确保系统的可靠性和可维护性。

颜淡慕潇 2万+

Sublime列对齐插件

演示: 源码: import sublime import sublime_plugin class AlignColCommand(sublime_plugin.TextCommand): def run(self, edit): most_right_pos = max(self.get_pos(self.view.sel())) for regio...

一叶知秋 2029

MIKE21 MIKE软件 教程 0.1 软件介绍与教学目录

MIKE 21是一个专业的工程软件包,用于模拟河流、湖泊、河口、海湾、海岸及海洋的水流、波浪、泥沙及环境问题。在MIKE Zero集成平台中,主要有MIKE11,MIKE21,MIKE3三类软件,分别对应一维,二维,三维的水动力模型模拟。同时还包括MIKE HYDRO,MIKE FLOOD等其他功能,各软件的具体介绍请参考DHI公司的官方文档,在此不作赘述。学习过程中,对于任何操作界面的疑问,建议大家仔细阅读帮助文档(在有疑问的界面按F1调出),同时官方也提供了对应的线上和线下教学服务(免费+收费)。

Hello Word 2万+

PowerShell获取文件个数

通过@转数组 @(Get-ChildItem).Count

一叶知秋 1589

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

VBA删除第一个sheet后的所有sheet

VBA删除第一个sheet后的所有sheet

一叶知秋 1480

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Python手记

python问题汇总

一叶知秋 1112

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

VBA按日期创建sheet

VBA按日期创建sheet

一叶知秋 1073

Python Logging Setup

import logging LOG_FMT = '[%(levelname)s] %(asctime)s: %(message)s' LOG_DATE_FMT = '%Y-%m-%d %H:%M:%S' logging.basicConfig(filename='search_results.log', format=LOG_FMT, datefmt=LOG_DATE_FMT, level=l...

一叶知秋 1028

Sublime Java Build -在Sublime中编译执行Java程序

{ "shell_cmd": ["javac", "$file_name", "&amp;&amp;", "java", "$file_base_name"], "shell": true, "working_dir": "$file_path", "selector&qu

一叶知秋 981

Python Create EPUB

import logging from ebooklib import epub import urllib3 import certifi from lxml import html import re log = logging.getLogger(__name__) log.setLevel(logging.INFO) log_handler = logging.StreamHandler() formatter = logging.Formatter('%(asctime)s - %(level

一叶知秋 948

Java核心类库(上)

Java核心类库(上) 参加拉勾教育大数据训练营课程笔记 学会查阅API文档,根据包索引,搜索等。 常用类 java.lang - 唯一一个Java虚拟机自动访问,所以System等不需要import就可以使用,例如:System, String等。 java.util - 提供大量工具类和集合类等,例如:Scanner , List, Random等。 java.io - 输入输出包,提供了文件读写,流读写等,例如:FileInputStream, FileOutputStream等。 java.n

一叶知秋 801

Java集合

Java集合 泛型编程 通常情况下集合中可以存放不同类型的对象,是因为将所有对象都看做Object类型放入的,因此从集合中取出元素时也是Object类型,为了表达该元素真实的数据类型,则需要强制类型转换,而强制类型转换可能会引发类型转换异常。 为了避免上述错误的发生,从Java5开始增加泛型机制,也就是在集合名称的右侧使用<数据类型>的方式来明确要求该集合中可以存放的元素类型,若放入其它类型的元素则编译报错。 泛型只在编译时期有效,在运行时期不区分是什么类型。 原理 泛型的本质就是类型参

一叶知秋 779

Spark手记

spark手记

一叶知秋 551

数据结构与算法复习-C语言回顾

C语言 数组 定义一维数组 类型符 数组名[常量表达式]; int a[10]; int b[5] = {1, 2, 3, 4, 5}; void func(int n) { int a[2 * n]; } 指定数组为静态static存储方式,则不能用“可变长数组” static int a[2 * n]; // 不合法,a数组指定为`static`存储方式 指针 存储地址的变量:类...

一叶知秋 547

Python Word Automation

# generate.py import win32com.client as win32 from datetime import datetime, date import re import os import sys import json import logging from enumerations import * logger = logging.getLogger(__name__) log_formater = logging.Formatter(fmt='%(asctime)s -

一叶知秋 496
上一篇: Scala手记
下一篇: NODEJS杂记
DataPeak
博客等级 码龄15年 32粉丝 68原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值