我想将 R 表达式解析为列表,并在最终将其转换为 json 对象之前选择性地修改它的各个方面。举个例子,我正在尝试实现以下目标:
{"op": "=",
"content": {
"lhs": "gender",
"rhs": ["male"]
}
}
Run Code Online (Sandbox Code Playgroud)
我将从 R 表达式开始,例如:
gender == "male"
Run Code Online (Sandbox Code Playgroud)
我可以用来pryr::ast获取树的文本版本,但我想将其作为列表获取,例如:
op: "=="
[[1]]: "gender"
[[2]]: "male"
Run Code Online (Sandbox Code Playgroud)
列表的“格式”细节并不那么重要,只要清楚即可。我的目标只是获得一个可计算且可修改的 R 表达式解析树。
我今天用一些生产代码遇到了这个问题,并且能够.toList()在等待之前通过一个简单的解决lazyList来修复它,但我不明白为什么它会以这种方式工作并且只在使用Future.wait()这里发生的事情时?为什么 lazyList 会被解析两次?
在 DartPad 上玩它(更改第doWait3 行的值并查看不同的结果)
import 'dart:async';
void main() {
var executedTracker = [];
var source = ["a", "b", "c"];
List promises = source.map((item) async {
print('executing item $item${(executedTracker.contains(item) ? ' (again!? o_O)' : '')}'); executedTracker.add(item);
return (item*2);
});
Future.wait(promises).whenComplete(() {
print('--------\nAll promises complete.');
print('Processing ${promises.length} results...\n');
promises.forEach((promise) => null /* do a thing with the result*/);
});
}
Run Code Online (Sandbox Code Playgroud)
executing item a
executing item b
executing item …Run Code Online (Sandbox Code Playgroud) 以下内容与其说是一个问题,不如说是一个评估请求。
因此,您很可能熟悉以下惰性 getter 模式。
private Object obj;
public Object getObject() {
if(obj==null) {
obj = new Object();
}
return obj;
}
Run Code Online (Sandbox Code Playgroud)
那个代码
所以最近一个同事和我想出了以下界面(简化):
public interface LazyGetterSupport {
default Object get(Supplier<Object> impl) {
String key = impl.getClass().getName();
Object retVal;
if ((retVal = getInstanceCache().get(key)) == null) {
retVal = impl.get();
getInstanceCache().put(key, retVal);
}
return retVal;
}
Map<String, Object> getInstanceCache();
}
Run Code Online (Sandbox Code Playgroud)
旁注:不使用 HashMap#computeIfAbsent bc of Bug-8071667
然后该接口由您要使用惰性获取器的类实现。您需要提供getInstanceCache()如下实现:
private Map<String, Object> instanceCache;
@Override
public Map<String, Object> getInstanceCache() …Run Code Online (Sandbox Code Playgroud) java getter lazy-loading lazy-evaluation lazy-initialization
我正在尝试在 Scala 中跟踪递归处理。以下是代码示例:
def factorial(n: Int): Int =
if (n <= 1) 1
else {
println("Computing factorial of " + n + " - I first need factorial of " + (n-1))
def result = n * factorial(n - 1)
println("Computed factorial of " + n)
result
}
println(factorial(3))
Run Code Online (Sandbox Code Playgroud)
以下是输出:
Computing factorial of 3 - I first need factorial of 2
Computed factorial of 3
Computing factorial of 2 - I first need factorial of 1
Computed factorial of 2 …Run Code Online (Sandbox Code Playgroud) algorithm recursion functional-programming scala lazy-evaluation
这段代码是做什么的?
fun<T:x>a.b(y: Int)=lazy{u.v<T>(y)}
Run Code Online (Sandbox Code Playgroud)
我不知道在 Kotlin 中什么是“懒惰”或者“懒惰”是什么特别的东西。
上下文:
Run Code Online (Sandbox Code Playgroud)def fib(n): if n < 2: return 1 return fib(n-1) + fib(n-2)可以通过记忆加速:
Run Code Online (Sandbox Code Playgroud)fib_memo = {} def fib(n): if n < 2: return 1 if not fib_memo.has_key(n): fib_memo[n] = fib(n-1) + fib(n-2) return fib_memo[n]这种记忆化的实现技术在许多编程语言中被广泛使用,但它不能直接应用于 Haskell,因为 Haskell 是纯的,我们不想为了记忆一个函数而引入杂质。幸运的是,由于 Haskell 的惰性求值特性,可以在没有副作用的情况下记住一个函数。
以下
memoize函数采用类型函数Int -> a并返回同一函数的记忆版本。诀窍是将函数转换为值,因为在 Haskell 中,函数不会被记忆,但值会被记忆。
问题:
haskell functional-programming memoization lazy-evaluation pure-function
I am working on a problem to read in a file with lines like:
A abcdefg
B bcdefgh
Run Code Online (Sandbox Code Playgroud)
But I keep getting errors about Lazy Sequence not compatible with Java Charseq ..
I tried:
(def notlazy (doall lyne2))
Run Code Online (Sandbox Code Playgroud)
Then thought I verified:
(realized? notlazy)
true
Run Code Online (Sandbox Code Playgroud)
But still:
(str/split notlazy #" ")
ClassCastException class clojure.lang.LazySeq cannot be cast to class
java.lang.CharSequence (clojure.lang.LazySeq is in unnamed module of
loader 'app'; java.lang.CharSequence is in module java.base of loader
'bootstrap') clojure.string/split (string.clj:219)
Run Code Online (Sandbox Code Playgroud)
Help please!
我有一些库,它具有使用 gnuplot 库进行绘图的功能:
import Graphics.Gnuplot.Simple
drawMap :: [(Int, Int)] -> IO ()
drawMap m = do
!a <- plotList [] m
print a
Run Code Online (Sandbox Code Playgroud)
我像这样从 main 调用这个函数:
main = do
!a <- drawMap [(1,2),(3,5)]
print a
Run Code Online (Sandbox Code Playgroud)
我用堆栈构建项目,并尝试了 -O2 和 -O0 优化,但 plot 永远print a不起作用(函数总是被成功调用并打印())。我怎样才能强制绘图以及为什么它不工作,使用库,但如果我只是plotList从 main调用就可以工作?
更新。
在 main 和drawMapby$!中使用严格的应用程序也不起作用:
drawMap :: [(Int, Int)] -> IO ()
drawMap m = plotList [] $! m
main = do
drawMap $! [(1,2),(3,5)]
Run Code Online (Sandbox Code Playgroud)
UPD 2 …
考虑以下 Haskell 代码
try_lex_rgx :: String -> IO (Maybe [RgxToken])
try_lex_rgx rgx_str =
catch
(do
rgx_toks <- evaluate $ lex_rgx rgx_str
return $ Just rgx_toks)
(\(LexerErr err_msg remainder) -> disp_lex_error rgx_str (LexerErr err_msg remainder))
Run Code Online (Sandbox Code Playgroud)
我打算让这段代码工作的方式是评估表达式lex_rgx rgx_str,在它们发生时捕获任何异常,然后调用disp_lex_error以漂亮地打印错误。
(顺便说一下,disp_lex_error的代码如下
disp_lex_error :: String -> RgxLexerException -> IO (Maybe [RgxToken])
disp_lex_error rgx_str (LexerErr err_msg remainder) = let loc_str_frag = "In regex " ++ rgx_str ++ " at ..." in
do
hPutStrLn stderr ("Lexer error: " ++ err_msg ++ "\n" ++ …Run Code Online (Sandbox Code Playgroud) 例如,如果我在同一个列表上应用映射(只是一个例子,也可以是过滤器或其他东西)3次,Haskell会遍历该列表三次还是只一次?在 ghci 中运行的示例:
map (+1) $ map (*2) $ map (^2) [1..100]
Run Code Online (Sandbox Code Playgroud)
我想知道复杂度是否为 3n,其中 n 是列表的大小,就像在命令式语言中一样,或者 Haskell 中的惰性是否通过仅遍历列表一次并执行这三个操作将其优化为 1n同时对每个元素进行操作。所以用 1 就可以了
然后继续处理下一个元素,而不是首先遍历整个列表,同时将每个元素提高到 2,然后再次遍历列表并将每个元素相乘,然后第三次遍历列表将每个元素增加一。
那么是哪一个呢?Haskell 是浏览该列表 3 次还是只浏览一次?
algorithm performance haskell functional-programming lazy-evaluation
lazy-evaluation ×10
haskell ×4
algorithm ×2
asynchronous ×1
catch-block ×1
clojure ×1
dart ×1
exception ×1
getter ×1
java ×1
kotlin ×1
lazy-loading ×1
memoization ×1
performance ×1
promise ×1
pryr ×1
r ×1
recursion ×1
scala ×1
syntax ×1