functional-programming

Converting a function to use tail recursion — a formal study

帅比萌擦擦* 提交于 2021-02-04 13:23:30
问题 Has anyone written a formal paper describing a method to (automatically) convert functions to be tail recursive? I am looking for a university-level formal treatment including the limitations (types of functions that can be converted), procedures for conversion, and, if possible, proofs of correctness? Examples in Haskell would be a bonus. 回答1: a method to (automatically) convert functions to be tail recursive? So there are two parts to this: recognizing that a given recursive function can be

Converting a function to use tail recursion — a formal study

浪尽此生 提交于 2021-02-04 13:23:05
问题 Has anyone written a formal paper describing a method to (automatically) convert functions to be tail recursive? I am looking for a university-level formal treatment including the limitations (types of functions that can be converted), procedures for conversion, and, if possible, proofs of correctness? Examples in Haskell would be a bonus. 回答1: a method to (automatically) convert functions to be tail recursive? So there are two parts to this: recognizing that a given recursive function can be

Filter a json data by another array in underscore.js

耗尽温柔 提交于 2021-02-04 08:40:16
问题 I have a search field and I want to add some complex functionality using underscore.js. Sometimes users search for a whole "sentence" like "Samsung galaxy A20s ultra". I want to filter JSON data using any of the words in the search string and sort by results that contain more of the words. Sample data: var phones = [ {name: "Samsung A10s", id: 845}, {name: "Samsung galaxy", id: 839}, {name: "Nokia 7", id: 814}, {name: "Samsung S20s ultra", id: 514}, {name: "Apple iphone ultra", id: 159},

How to extract types from a tuple that implements a typeclass

谁都会走 提交于 2021-01-29 13:06:05
问题 Function a can receive single argument or a tuple, these arguments need to be members of typeclass StringIdentifiable How to extract and decompose tuple type into types that also have instances of the typeclass @typeclass trait StringIdentifiable[M] { def identify(id: M): String } def a[K: StringIdentifiable] (k:K){ k match{ case (k1) => implicitly[StringIdentifiable[K]].identify(k1) case (k1,k2) => implicitly[StringIdentifiable[k1.type]].identify(k1) implicitly[StringIdentifiable[k2.type]]

Update a counter from a list of counters without a loop?

你说的曾经没有我的故事 提交于 2021-01-29 08:01:15
问题 I have a list of counters: from collections import Counter counters = [ Counter({"coach": 1, "says": 1, "play": 1, "basketball": 1}), Counter({"i": 2, "said": 1, "hate": 1, "basketball": 1}), Counter({"he": 1, "said": 1, "play": 1, "basketball": 1}), ] I can combine them using a loop as shown below, but I'd like to avoid the loop. all_ct = Counter() for ct in counters: all_ct.update(ct) Using reduce gives an error: all_ct = Counter() reduce(all_ct.update, counters) >>> TypeError: update()

How map work on Options in Scala?

烂漫一生 提交于 2021-01-28 11:44:59
问题 I have this two functions def pattern(s: String): Option[Pattern] = try { Some(Pattern.compile(s)) } catch { case e: PatternSyntaxException => None } and def mkMatcher(pat: String): Option[String => Boolean] = pattern(pat) map (p => (s: String) => p.matcher(s).matches) Map is the higher-order function that applies a given function to each element of a list. Now I am not getting that how map is working here as per above statement. 回答1: Map is the higher-order function that applies a given

Python: Redefine function so that it references its own self

与世无争的帅哥 提交于 2021-01-28 09:47:38
问题 Say I have got some function fun , the actual code body of which is out of my control. I can create a new function which does some preprocessing before calling fun , i.e. def process(x): x += 1 return fun(x) If I now want process to take the place of fun for all future calls to fun , I need to do something like # Does not work fun = process This does not work however, as this creates a cyclic reference problem as now fun is called from within the body of fun . One solution I have found is to

LeanFT C# automation; Clicking on a wpf button control throwing exception

大城市里の小女人 提交于 2021-01-28 06:08:21
问题 Getting HP.LFT.SDK.GeneralReplayException: One or more specified arguments are not valid , while trying to click on a wpf button (using LeanFT with C# integrated in Visual Studio 2015 ) Given the code below: // Identify the "LicensingButton" button var LicensingButton = objAdminApplicationModel.wnd_Adminstration.Describe<IButton>(new ButtonDescription { Text = @"Licensing", ObjectName = @"Licensing" }); // Click the Licensing button. LicensingButton.Click(); But I am getting below exception

Why does this not evaluate in Scheme?

元气小坏坏 提交于 2021-01-28 05:40:48
问题 I am using the DrRacket environment to try out the Scheme language. I defined sum+1 as follows: (define sum+1 '(+ x y 1)) I was wondering why the following expression does not evaluate: (let ([x 1] [y 2]) (eval sum+1)) whereas doing this returns the correct value: (define x 1) (define y 2) (eval sum+1) 回答1: eval does not work with lexical variables at all unless the lexical variable was created in the same expression: #!r7rs (import (scheme base) (scheme eval)) (define env (environment '

Infer parameter types in function chain

北慕城南 提交于 2021-01-28 05:12:09
问题 I'm trying to make a home made function mapping chain. Thing is, I want to make sure typing stays consistent trough the mapping chain. My problem is that I don't know how to write f(x:T) => U For a proper example of what I'm trying to do: function myChainer<T>(args:T){ return { map:(innerFuction:(payload: T) => unknown){ return myChainer(innerFuction(args)) } } } Now, if I run myChainer(0) .map((args1) => { return doSomething(args1) ? "a" : "b" }) .map((args2) => { return doSomething2(args2)