functional-programming

Specify arity using only or except when importing function on Elixir

ⅰ亾dé卋堺 提交于 2019-12-18 07:38:09
问题 I'm studying Elixir and when I use only or except operators when importing functions from a module I need to specify an arity number. Why? e.g. import :math, only: [sqrt: 1] or import :math, except: [sin: 1, cos: 1] 回答1: Across the Erlang ecosystem functions are identified by name + arity. In most other languages you can overload functions by name. In other words, in the Erlang world foo/1 (that is, foo(one_arg)) is a completely different function than foo/2 (as in, foo(one_arg, two_arg)),

React router v4 - Authorized routes with HOC

耗尽温柔 提交于 2019-12-18 07:14:37
问题 I have a problem to prevent unauthorized users from accessing authorized-only routes/components - such as logged in users dashboard I have the following code: import React from 'react' //other imports import {withRouter} from 'react-router' class User extends React.Component { constructor(props) { super(props) console.log('props', props) let user = JSON.parse(localStorage.getItem('userDetails')) if(!user || !user.user || props.match.params.steamId !== user.user.steamId) { props.history.push('

How to define division operator in Agda?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 06:59:47
问题 I want to divide two natural number. I have made function like this _/_ : N -> N -> frac m / one = m / one (suc m) / n = ?? I dont know what to write here. Please help. 回答1: As @gallais says you can use well-founded recursion explicitly, but I don't like this approach, because it's totally unreadable. This datatype record Is {α} {A : Set α} (x : A) : Set α where ¡ = x open Is ! : ∀ {α} {A : Set α} -> (x : A) -> Is x ! _ = _ allows to lift values to the type level, for example you can define a

The differences between underscore usage in these scala's methods

末鹿安然 提交于 2019-12-18 06:08:56
问题 What is the difference and the term name between these underscore usage from these codes: (see the handler(resource) part) 1. def readFile[T](f: File)(handler: FileInputStream => Byte => T): T = { val resource = new java.io.FileInputStream(f) try { val hh = handler(resource)_ hh(2) } finally { resource.close() } } val bs = new Array[Byte](4) readFile(new File("scala.txt")) { input => b: Byte => println("Read: " + (input.read(bs) + b)) } I got compile error: Error:(55, 29) _ must follow method

Mapping a FunctionalJava Option<Type> with Hibernate

柔情痞子 提交于 2019-12-18 05:59:10
问题 I have a hibernate-mapped Java object, JKL , which is full of a bunch of normal hibernate-mappable fields (like strings and integers). I'm added a new embedded field to it (which lives in the same table -- not a mapping), asdf , which is a fj.data.Option<ASDF> . I've made it an option to make it clear that this field may not actually contain anything (as opposed to having to handle null every time I access it). How do I set up the mapping in my JKL.hbm.xml file? I'd like hibernate to

How to shuffle list in O(n) in OCaml?

送分小仙女□ 提交于 2019-12-18 05:56:50
问题 It is not hard to shuffle an array in O(n), with in place swapping, How to do it for list in OCaml, with O(n)? Requirement: No array or in place usage Consider this as an interview question 回答1: Lists are immutable, and there's often a log n price to pay for working with immutable data. If you're willing to pay this cost, there's an obvious n log n approach: tag each list element with a random value, sort based on random value, remove random values. This is the way I shuffle lists in my

How do you copy an array in common lisp?

人走茶凉 提交于 2019-12-18 05:45:20
问题 I'd like to make copies of my 2D array, which feels like the nice, functional, nondestructive way of handling arrays. What is the lispy way of doing this? 回答1: UPDATE : Nowadays, alexandria has a copy-array very similar to the implementation given below. Use that. OBSOLETE ANSWER : I used the following, which I believed was better than the alexandria version at the time: (defun copy-array (array &key (element-type (array-element-type array)) (fill-pointer (and (array-has-fill-pointer-p array)

F# - Function with no arguments?

笑着哭i 提交于 2019-12-18 05:27:25
问题 When thinking in a functional mindset, given that functions are supposed to be pure, one can conclude any function with no arguments is basically just a value. However, reallity gets in the way, and with different inputs, I might not need a certain function, and if that function is computationally expensive, I'd like to not evaluate it if it's not needed. I found a workaround, using let func _ = ... and calling it with func 1 or whatever, but that feels very non-idiomatic and confusing to the

Most concise way to combine sequence elements

一个人想着一个人 提交于 2019-12-18 04:50:58
问题 Say we have two sequences and we and we want to combine them using some method val a = Vector(1,2,3) val b = Vector(4,5,6) for example addition could be val c = a zip b map { i => i._1 + i._2 } or val c = a zip b map { case (i, j) => i + j } The repetition in the second part makes me think this should be possible in a single operation. I can't see any built-in method for this. I suppose what I really want is a zip method that skips the creation and extraction of tuples. Is there a prettier /

Where does the performance boost of map or list comprehension implementations over calling a function over a loop come from?

纵然是瞬间 提交于 2019-12-18 04:26:18
问题 I understand that you could be more efficient with memory in the implementation of map than in how you might do it over a loop. However, I see that using a map function over calling a function iterating over a loop has a speed boost as well. Is this coming from some optimization in where to store the memory? One example of what I mean by this is placement of memory done in such a way that it is contiguous. Also I can see that if the operations were being in run in parallel then there would