在单行中缓存一个中间变量 [英] Cache an intermediate variable in an one-liner

查看:39
本文介绍了在单行中缓存一个中间变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在这个简单的函数定义中以某种方式缓存 i.toString 吗?

Can I somehow cache the i.toString in this simple definition of function?

def palindrome(i: Int) = i.toString == i.toString.reverse

我想让这个函数保持简单,没有经典的多行、大括号括起来的函数..

I want to keep this function simple, w/o a classic multi-line, brace-enclosed function..

推荐答案

您可以:

def palindrome(i: Int) = ((s:String) => s == s.reverse)(i.toString)

这篇关于在单行中缓存一个中间变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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