functional-programming

Sorting List with OCaml standard library function

北慕城南 提交于 2019-12-24 03:52:31
问题 I'm studying OCaml and and doing various exercises on ordering data. I would like to understand how to use the standard librari List for ordering For example I would like to sort this array using these functions [94; 50; 6; 7; 8; 8] List.sort List.stable_sort List.fast_sort List.unique_sort What is the syntax to do it ? 回答1: If you want to use these functions on your list, you have to specifiy the comparison function. Quote from the documentation: The comparison function must return 0 if its

Sorting List with OCaml standard library function

*爱你&永不变心* 提交于 2019-12-24 03:52:13
问题 I'm studying OCaml and and doing various exercises on ordering data. I would like to understand how to use the standard librari List for ordering For example I would like to sort this array using these functions [94; 50; 6; 7; 8; 8] List.sort List.stable_sort List.fast_sort List.unique_sort What is the syntax to do it ? 回答1: If you want to use these functions on your list, you have to specifiy the comparison function. Quote from the documentation: The comparison function must return 0 if its

how to generically handle Scala Higher-kinded types when coding factories for generating containers of items of type 'X'

廉价感情. 提交于 2019-12-24 03:27:23
问题 After working through some examples of Scala Higher-kinded types in this tutorial, I started wondering if it is possible to write a method that generically handles two subclasses of a trait that is defined as a higher-kinded type. The tutorial defines (a slightly more complex version of) this trait: trait ContainerFactory[M[_]] { def put[A](x: A): M[A] } Which I understand as the signature of a type parameterized factory that creates different kinds of containers ( Lists , Set s, etc., where

Shortest subarray containing all elements without using arrays?

删除回忆录丶 提交于 2019-12-24 03:19:00
问题 Problem: Find the length of the shortest subarray that contains all elements Example : 1 2 2 3 2 2 1 3 Answer : 3 I have read that the best approach to this problem is by using the sliding window approach. But this approach requires using arrays. Is there any other efficient approach that does not require to use arrays by storing the number of appearances of each element? ( I would like to use this approach without arrays by writing it in ML ) 回答1: I assume that the reason you want to avoid

Assigning RegExp.test to a variable

僤鯓⒐⒋嵵緔 提交于 2019-12-24 02:28:11
问题 The following code: var r = /^[0-9A-Z]$/.test; r("A") Throws 'TypeError: can't convert undefined to object' How else could I assign the test function to a variable, for passing in functions, later evaluation, etc.? (Without wrapping the regex in another function) Update: Consider this bit of valid code before answering: var o = { f: function() { return 1 } }; var a = o.f; var b = a(); // b = 1 回答1: It has to do with the value of this inside the test method. For example: var obj = { method:

Why OCaml does not allow function matching? [closed]

a 夏天 提交于 2019-12-24 02:04:49
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . if I do match (fun i -> i + 1) with (fun i -> i + 1) -> true;; It got rejected. Why OCaml does not allow function matching? 回答1: There

Why OCaml does not allow function matching? [closed]

℡╲_俬逩灬. 提交于 2019-12-24 02:04:03
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . if I do match (fun i -> i + 1) with (fun i -> i + 1) -> true;; It got rejected. Why OCaml does not allow function matching? 回答1: There

How can I use functools.partial on multiple methods on an object, and freeze parameters out of order?

守給你的承諾、 提交于 2019-12-24 01:38:19
问题 I find functools.partial to be extremely useful, but I would like to be able to freeze arguments out of order (the argument you want to freeze is not always the first one) and I'd like to be able to apply it to several methods on a class at once, to make a proxy object that has the same methods as the underlying object except with some of its methods parameters being frozen (think of it as generalizing partial to apply to classes). And I'd prefer to do this without editing the original object

What is the most concise way to include functions or lambdas as conditions in a Kotlin `when` statement (or other branching construct)?

自古美人都是妖i 提交于 2019-12-24 01:37:29
问题 I'm processing strings, and I came across the Regex or Wildcard answer: that one can put regular expressions in a when statement with a custom class that overrides equals. While this does effectively use the type system to shoehorn syntactic sugar into the when statement, I find the following pretty ugly, and would never do this in code that I intend to share with another developer (quoting travis): import kotlin.text.regex when (RegexWhenArgument(uri)) { Regex(/* pattern */) -> /* do stuff *

pure function of functions that returns functions in D

浪子不回头ぞ 提交于 2019-12-24 01:23:18
问题 I'm trying to create a pure function that returns the multiplication of two other pure functions: pure Func multiplyFunctions(Func,Real)(scope const Func f1, scope const Func f2) { return (Real a) { return f1(a) * f2(a); }; } Unfortunately, I'm running into problems, number one, I want to declare f1 and f2 to be pure functions/delegates/classes with opCall defined... which is required because I'm calling them from a pure function. But number two, and what seems to be the most problematic, is