higher-order-functions

Plot two functions on the same graph in MATLAB using ezplot

*爱你&永不变心* 提交于 2019-12-23 12:06:12
问题 i would like to plot the following three functions in MATLAB using ezplot() but i want the functions to be on the same graph to easily interpret the differences. is this possible? if so how? the three function are: x^3 x^5 x^7 thanks, mysticxhobo 回答1: Just use hold on to plot them in succession on the same axes: figure; hold on; ezplot('x^3'); ezplot('x^5'); ezplot('x^7'); 来源: https://stackoverflow.com/questions/6602982/plot-two-functions-on-the-same-graph-in-matlab-using-ezplot

map in python2 vs python3

。_饼干妹妹 提交于 2019-12-23 09:56:55
问题 I'm a beginner python user and I've ran the following code on both python2.7 and python3.4.3 import matplotlib.pyplot as plt import numpy as np import scipy.stats as stats alpha = 1 n = 100 u = stats.uniform(0,1) F_inverse = lambda u: 1/alpha*np.log(1/(1-u)) v = np.array(map(F_inverse, u.rvs(n))) print(v) fig, ax = plt.subplots(1,1) stats.probplot(v, (1,), dist='expon', plot=ax) plt.show() On python2 i get a nice array like this: array([ 2.29133808e+00, 1.63236151e+00, 6.77776227e-01, 3

Concise syntax for function composition in Scala?

让人想犯罪 __ 提交于 2019-12-23 08:30:22
问题 I'm learning Scala and ran across the following task - if string is blank then return null, otherwise uppercase it. There are two functions in Apache Commons that composed together solves the problem. In Haskell I'd just write: upperCaseOrNull = StringUtils.stripToNull . StringUtils.upperCase However I can't find a way to do an easy and clean function composition in Scala. the shortest way I found is as follows: def upperCaseOrNull (string:String) = StringUtils.stripToNull (StringUtils

How to do Nested For Loops in Functional Style

你。 提交于 2019-12-22 10:16:50
问题 I'm in the process of learning functional programming, and completely getting rid of for loops has been a challenge sometimes, because they provide so much control and freedom. Below is an example of checking if a string is an isogram or not (no letters should be repeated). With nested for loops, it became an easy solution. Is there a way to do this the functional way with any high order functions or anything else? Any suggestion would be a huge help. Code: function isIsogram(string) { let

Higher-order functions in C as a syntactic sugar with minimal effort

谁说胖子不能爱 提交于 2019-12-22 05:25:25
问题 I want to implement higher-order functions (HOFs) in C as a syntactic sugar with minimal effort. For example, for the following code function add(int x) { return int(int y) { return x + y; }; } int main() { function add1 = add(1); return add1(2); } it is transcompiled into pure C as #include <stdlib.h> typedef struct { void *ctx; void* (*fun)(void *arg, void *ctx); } function; function new_function(void *ctx, void* (*fun)(void *, void *)) { function f = {.ctx=ctx, .fun=fun}; return f; } void*

Implicit conversion of a function to a second-order-function only works if the function to convert has at least two parameters

。_饼干妹妹 提交于 2019-12-21 12:12:53
问题 I have a problem of implicit conversions and higher-order functions. It seems that an implicit conversions of a function to a second-order-function only works, if the function to convert has at least two parameters. Works: implicit def conv(foo: Integer => String): String => String = null Does not work: implicit def conv(foo: Integer => String): String => String => String = null Works: implicit def conv(foo: (Integer, Integer) => String): String => String => String = null Full example with

Haskell FlatMap

a 夏天 提交于 2019-12-21 07:31:50
问题 I am a beginner interested in Haskell, and I have been trying to implement the flatmap (>>=) on my own to better understand it. Currently I have flatmap :: (t -> a) -> [t] -> [a] flatmap _ [] = [] flatmap f (x:xs) = f x : flatmap f xs which implements the "map" part but not the "flat". Most of the modifications I make result in the disheartening and fairly informationless Occurs check: cannot construct the infinite type: a = [a] When generalising the type(s) for `flatmap' error. What am I

Higher order functions with Scala Slick for DRY goodness

可紊 提交于 2019-12-21 05:14:20
问题 I have an idea how my data access layer with Scala Slick should look like, but I'm not sure if it's really possible. Let's assume I have a User table which has the usual fields like id, email, password, etc. object Users extends Table[(String, String, Option[String], Boolean)]("User") { def id = column[String]("id", O.PrimaryKey) def email = column[String]("email") def password = column[String]("password") def active = column[Boolean]("active") def * = id ~ email ~ password.? ~ active } And I

Multiple folds in one pass using generic tuple function

≡放荡痞女 提交于 2019-12-20 20:38:09
问题 How can I write a function which takes a tuple of functions of type ai -> b -> ai and returns a function which takes a tuple of elements of type ai , one element of type b , and combines each of the elements into a new tuple of ai : That is the signature should be like f :: (a1 -> b -> a1, a2 -> b -> a2, ... , an -> b -> an) -> (a1, a2, ... , an) -> b -> (a1, a2, ... , an) Such that: f (min, max, (+), (*)) (1,2,3,4) 5 = (1, 5, 8, 20) The point of this is so I can write: foldlMult' t = foldl'

What are some interesting uses of higher-order functions?

≡放荡痞女 提交于 2019-12-20 08:01:18
问题 I'm currently doing a Functional Programming course and I'm quite amused by the concept of higher-order functions and functions as first class citizens. However, I can't yet think of many practically useful, conceptually amazing, or just plain interesting higher-order functions. (Besides the typical and rather dull map , filter , etc functions). Do you know examples of such interesting functions? Maybe functions that return functions, functions that return lists of functions (?), etc. I'd