重构/布局的功能Scala [英] Refactoring / layout of functional Scala

查看:102
本文介绍了重构/布局的功能Scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一行...

 Console.println(io.Source.fromFile("names.txt").getLines.mkString.split(",").map{x:String => x.slice(1, x.length -1)}.sortBy { x => x}.zipWithIndex.map{t =>{ (t._2 +1)*(t._1.map{_.toChar - "A"(0).toChar + 1}.sum)}}.sum);

...是我的解决方案 Project Euler问题22 。它似乎工作,它写在(我的尝试)功能风格。

... is my solution to Project Euler problem 22. It seems to work, and it's written in (my attempt at) functional style.

这个例子有点极端,但我的问题是一个更一般 - 你喜欢写/格式/注释功能样式代码吗?函数式方法似乎鼓励了一系列方法调用,我发现这些方法调用可能无法读取,并且也没有明显的注释。

This example is a bit extreme, but my question is a bit more general - how do you prefer to write/format/comment functional style code? The functional approach seems to encourage a sequence of method calls, which I find can get unreadable, and also leaves nowhere obvious to put comments.

此外,当我编写程序代码,我发现我写的小方法每个有一个目的和有意义的名字。当我写功能代码,我似乎正在发展一种习惯,生产线有点像上面的一个,在那里(我)的意思是很难解密 - 而且个别计算很难在其他地方重复使用。相当多的功能代码示例我在网上看到的都是相似的(对我)晦涩。

Also, when I write procedural code, I find I write small methods each with one purpose and with meaningful names. When I write functional code, I seem to be developing a habit that produces lines a little like the one above, where (to me) the meaning is difficult to decipher - and also the individual computations are difficult to re-use elsewhere. Quite a lot of the functional code examples I see on the web are similarly terse (to me) obscure.

我应该怎么办?使用在当前上下文中有意义的名称为计算的每个部分编写小函数? (即使他们只是一个地图的包装器,说?)

What should I be doing? Writing little functions for each part of the computatation with names meaningful in the current context? (even if they're little more than a wrapper for map, say?)

对于我给出的例子,什么是更好的写法,

For the example I've given, what are better ways of writing it, and presenting it?

推荐答案

一个简单的尝试,只是删除领导控制台 c>; :类型 - 所有这些都是不必要的 - 添加一些缩进和导入io.Source:

A trivial first-attempt at tidying it up is to just remove the leading Console. the trailing ; and the explicit :String type - all of which are unnecessary - add some indentation and import io.Source:

import io.Source
println(
  Source.fromFile("names.txt").getLines.mkString.split(",").map{
    x => x.slice(1, x.length -1)
  }.sortBy {x => x}.zipWithIndex.map{
    t =>{ (t._2 +1)*(t._1.map{_.toChar - "A"(0).toChar + 1}.sum)}
  }.sum
)

下一步是清理它一点,当映射到元组列表时使用模式匹配, code> identity 而不是 x => x toChar 对于字符也是不必要的,单引号可用于表示字符文字。

The next step is to clean it up a little, use pattern matching when mapping over list of tuples and identity instead of x=>x. toChar is also unnecessary for characters, and single quotes can be used to represent character literals.

import io.Source
println(
  Source.fromFile("names.txt").getLines.mkString.split(",").map {
    x => x.slice(1, x.length -1)
  }.sortBy(identity).zipWithIndex.map {
    case (v, idx) =>{ (idx+1)*(v.map{_ - 'A' + 1}.sum)}
  }.sum
)

还有一些更改也有助于使代码的意图变得更加清楚:

A few more changes also help make the intent of the code far clearer:

import io.Source
println(
  Source.fromFile("names.txt").getLines.mkString.split(",")
  .map { _.stripPrefix("\"").stripSuffix("\"") }
  .sortBy(identity)
  .map { _.map{_ - 'A' + 1}.sum }
  .zipWithIndex
  .map { case (v, idx) => (idx+1) * v }
  .sum
)

下一步,为了使它更功能,将它分成函数( sneaky,huh?)。理想情况下,每个函数都有一个明确表达其目的的名称,它将是短的(目的是它是一个单一的表达式,因此不需要大括号):

The next step, to make it more "functional", is to break it into "functions" (sneaky, huh?). Ideally each function will have a name clearly expressing its purpose, and it will be short (aim for it to be a single expression, so braces aren't required):

import io.Source

def unquote(s:String) = s.stripPrefix("\"").stripSuffix("\"")

def wordsFrom(fname:String) =
  Source.fromFile(fname).getLines.mkString.split(",").map(unquote)

def letterPos(c:Char) = c - 'A' + 1

println(
  wordsFrom("names.txt")
  .sortBy(identity)
  .map { _.map(letterPos).sum }
  .zipWithIndex
  .map { case (v, idx) => (idx+1) * v }
  .sum
)

wordsFrom ,但我拆分它更容易格式化在stackOverflow

wordsFrom is an obvious 1-liner, but I split it for easier formatting on stackOverflow

这篇关于重构/布局的功能Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆