看看这个简单的扩展函数,我有中缀:
infix fun View.isValidColor(hexColor: String?): Boolean {
var isValid = true
return hexColor?.let {
try {
Color.parseColor(it)
} catch (e: Throwable) {
isValid = false
}
isValid
} ?: false
}
//notice how i have infix the extension meaning brackets are not needed, hopefully making it easier to read.
Run Code Online (Sandbox Code Playgroud)
现在让我们看看用法和我尝试过的:
它不是中缀,它遵循中缀规则:
我究竟做错了什么 ?
既然现在我使用显式对象,为什么会失败?ivLogo 是从 kotlin 合成的 ImageView。
我正在编写一个小型计算器(带有前缀表示法),我很好奇如何将前缀表示法转换为中缀表示法。我目前有一个功能,但它很奇怪,我不知道如何修复它。我所说的奇怪是指如果给予['+', x, y]它就会返回,(() + x + () + y)这让我感到困惑。这是代码。
def pre_in(read):
#print read
tempOp = read[0]
body = read[1:]
expr = []
for i in range(len(body)-1):
if not isinstance(body[i], list) and body[i] != " ":
expr.append(str(body[i]))
expr.append(tempOp)
else:
expr.append(str(pre_in(body[i])))
expr.append(tempOp)
try:
if not isinstance(body[-1], list):
expr.append(str(body[-1]))
else:
expr.append(str(pre_in(body[-1])))
except:
pass
if expr != None: return "("+' '.join(expr)+")"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我想将一个函数定义为中缀,这样用户就不必手动用反引号来包围该函数来调用它.具体来说,我正在编写类似DSL的功能,接受Rank和Suit并构建扑克牌记录:
-- pseudocode
data PokerCard = PokerCard { rank :: Rank, suit :: Suit } deriving (Eq)
of :: Rank -> Suit -> PokerCard
r of s = PokerCard { rank = r, suit = s }
pokerDeck = [
Ace of Spades,
Two of Spades,
...
]
Run Code Online (Sandbox Code Playgroud)
我相信,of保留为语法case ... of表达,所以我不得不重新命名它像of',.of,+of等等.
给定这样的输入:3+4+
算法将其转换为3 4 + +
我可以在执行后缀表达式时找到错误.但是,有可能在转换期间发现这一点吗?
(我读过的维基百科文章和网络文章不处理这种情况)
谢谢
数学表达式通常用中缀表示法表示.出于评估目的,我们可以将其更改为postfix(反向抛光)表示法(使用Shunting-Yard等算法),然后使用堆栈评估后缀表示法.
我发现计算器使用这种技术,但今天的现代编译器是否使用它进行算术表达式评估?它是否足够有效或正在使用其他技术(或算法)?
compiler-construction infix-notation expression-evaluation shunting-yard
我正在尝试实现一个前向管道功能,比如bash |或R最近%>%.我已经看到了这个实现http://dev-tricks.net/pipe-infix-syntax-for-python,但这要求我们事先定义可能与管道一起使用的所有函数.在寻找完全一般的东西时,这是我到目前为止所想到的.
此函数将其第一个参数应用于其第二个参数(函数)
def function_application(a,b):
return b(a)
Run Code Online (Sandbox Code Playgroud)
例如,如果我们有一个平方函数
def sq(s):
return s**2
Run Code Online (Sandbox Code Playgroud)
我们可以用这种繁琐的方式调用该函数function_application(5,sq).为了更接近前向管道,我们希望使用function_application中缀表示法.
由此绘制,我们可以定义一个Infix类,以便我们可以将函数包装在特殊字符中,例如|.
class Infix:
def __init__(self, function):
self.function = function
def __ror__(self, other):
return Infix(lambda x, self=self, other=other: self.function(other, x))
def __or__(self, other):
return self.function(other)
Run Code Online (Sandbox Code Playgroud)
现在我们可以定义我们的管道,它只是函数的中缀版本function_application,
p = Infix(function_application)
Run Code Online (Sandbox Code Playgroud)
所以我们可以做这样的事情
5 |p| sq
25
Run Code Online (Sandbox Code Playgroud)
要么
[1,2,3,8] |p| sum |p| sq
196
Run Code Online (Sandbox Code Playgroud)
在那个冗长的解释之后,我的问题是,是否有任何方法可以覆盖有效函数名称的限制.在这里,我已经命名了管道p,但是可以重载非字母数字字符吗?我可以命名一个功能,>所以我的管道是|>|?
请考虑以下场景:我有一个类Test
class Test() {
infix fun say(msg: String) = println(msg)
}
Run Code Online (Sandbox Code Playgroud)
和一个主要方法
fun main(args: Array<String>) {
val test = Test()
test say "Hello World!" //Works
with(test) {
say "Goodbye World!" //Does not work
say("Hello again!") //Works
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在测试中缀表示法.考虑with(...)允许您使用在with块中作为参数传递的对象,而不必通过点表示法访问其成员,我希望中缀符号的工作方式与我在上面的示例中显示的一样.
不幸的是,这不起作用,这有什么原因不起作用?这是一个错误还是仅仅是一个限制?或者我可能没有with(...)正确地解释这个功能?
我想要做的是使用infix fmap(我已经定义为<^>)来处理多种类型,例如Option和Either(自定义类型).
鉴于:
type Either<'a, 'b> = Left of 'a | Right of 'b
Run Code Online (Sandbox Code Playgroud)
在代码中我希望能够做到:
let fO (a : int option) = None
let fE (a : Either<string,int>) = Left "dummy"
let mO = Some 1
let mE = Right 1
let testO = f0 <^> m0
let testE = fE <^> mE
Run Code Online (Sandbox Code Playgroud)
每个地方(<^>):
let (<^>) f m = match m with | Some a -> Some <| f a | None -> None
let (<^>) f m = match …Run Code Online (Sandbox Code Playgroud) 我正在测试一个中缀到后缀到中缀的转换器,并发现了一些不确定性.例如,一个简单的中缀和
1 + 2 + 3 + 4
Run Code Online (Sandbox Code Playgroud)
可以转换为后缀一个
1 2 + 3 + 4 +
Run Code Online (Sandbox Code Playgroud)
假设没有累积优先级相同的运算符.如果他们是我那么
1 2 3 4 + + +
Run Code Online (Sandbox Code Playgroud)
另一方面,所有以下后缀表达式都可以转换为初始总和
1 2 + 3 + 4 +
1 2 + 3 4 + +
1 2 3 4 + + +
Run Code Online (Sandbox Code Playgroud)
所有这些后缀表达式都是正确的吗?
如果您要制作这样的转换器,您会选择哪种形式?我需要选择一个进行测试.
algorithm infix-notation notation postfix-notation shunting-yard
采用单个参数的方法可以写为Scal中的中缀运算符.即添加*(other:C) = foo(this, other)到C类,将允许我们写c1 * c2而不是foo(c1,c2).但是有没有办法在现有的类上定义中缀操作符,而这些操作符无法修改?
例如,如果我想写c1 + c2而不是xor(c1,c2),在哪里c1,c2:Array[Byte],我显然无法修改Array-Class.
我发现了这个并尝试过
implicit class Bytearray(a1:Array[Byte]) extends Anyval {
def +(a2:Array[Byte]) = xor(a1,a2)
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用(c1 + c2).
类型不匹配,预期:字符串,实际:数组[字节]
我想也许这个问题是我使用的+,所以我交换了它,xor
但c1 xor c2只是导致了
无法解析符号xor
有什么建议?
UPDATE
有趣.我有一个class Foo与object Foo它下面的定义中,包含隐含的类.这导致上述错误.
但是,删除对象,而是将隐式类放入a trait BytearrayHandling然后扩展它(class Foo extends BytearrayHandling)似乎工作.这是为什么?
scala operators infix-notation infix-operator implicit-conversion