Search Results

Search found 5123 results on 205 pages for 'functional dependencies'.

Page 41/205 | < Previous Page | 37 38 39 40 41 42 43 44 45 46 47 48  | Next Page >

  • By-name repeated parameters

    - by Green Hyena
    How to pass by-name repeated parameters in Scala? The following code fails to work: scala> def foo(s: (=> String)*) = { <console>:1: error: no by-name parameter type allowed here def foo(s: (=> String)*) = { ^ Is there any other way I could pass a variable number of by name parameters to the method?

    Read the article

  • scheme2lisp::define function and pass it as parameter

    - by Stas
    Hi! Im need translate some code from scheme to common lisp. Now I have something like this (defun sum (term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) b)))) (defun sum-int (a b) (defun (ident x) x ) (sum ident a 1+ b)) But it doesn't interprete with out errors. * - DEFUN: the name of a function must be a symbol, not (IDENT X) Help me plese. Thanks

    Read the article

  • C++ Program Flow: Sockets in an Object and the Main Function

    - by jfm429
    I have a rather tricky problem regarding C++ program flow using sockets. Basically what I have is this: a simple command-line socket server program that listens on a socket and accepts one connection at a time. When that connection is lost it opens up for further connections. That socket communication system is contained in a class. The class is fully capable of receiving the connections and mirroring the data received to the client. However, the class uses UNIX sockets, which are not object-oriented. My problem is that in my main() function, I have one line - the one that creates an instance of that object. The object then initializes and waits. But as soon as a connection is gained, the object's initialization function returns, and when that happens, the program quits. How do I somehow wait until this object is deleted before the program quits? Summary: main() creates instance of object Object listens Connection received Object's initialization function returns main() exits (!) What I want is for main() to somehow delay until that object is finished with what it's doing (aka it will delete itself) before it quits. Any thoughts?

    Read the article

  • Problem with pattern matching in ocaml

    - by Antony
    I wrote the function used to decompose a Boolean function, the problem is that the compilation I get this : "Warning 5: this function application is partial, maybe some arguments are missing." How can I solve this problem? I've set wrong the patter matching or I can not run this operation with pattern matching The code is the following: let rec decomposition state_init state prec formula = match formula with And form -> (fun () -> let f1 = List.hd form in let f2 = And(List.tl form )in let new_state = Forms (state_init,f1) in decomposition state_init new_state state f1; decomposition state_init new_state state f2; Hashtbl.add graph new_state (("",false,state :: []) , []) ; let x = Hashtbl.find graph state in let succ = state :: snd x in let (desc,last,ptrs) = fst x in Hashtbl.replace graph state ( ("And-node",last,ptrs) , succ))

    Read the article

  • Right rotate of tree in Haskell: how is it work?

    - by Roman
    I don't know haskell syntax, but I know some FP concepts (like algebraic data types, pattern matching, higher-order functions ect). Can someone explain please, what does this code mean: data Tree ? = Leaf ? | Fork ? (Tree ?) (Tree ?) rotateR tree = case tree of Fork q (Fork p a b) c -> Fork p a (Fork q b c) As I understand, first line is something like Tree-type declaration (but I don't understand it exactly). Second line includes pattern matching (I don't understand as well why do we need to use pattern matching here). And third line does something absolutely unreadable for non-haskell developer. I've found definition of Fork as fork (f,g) x = (f x, g x) but I can't move further anymore.

    Read the article

  • Should I invest time in learning Java language these days? (question from a greenhorn)

    - by dave-keiture
    Hi experts, Assuming you've already had a chance to look through the lambda syntax proposed for Java7 (and the other things that have happened with Java, after Oracle has bought Sun + obvious problems in Java Community Process), what do you think is the future of Java language? Should I, as a Java greenhorn, invest time in learning Java language (not talking about the core JVM, which definitely will survive anything, and worth investments), or concentrate on Scala, Groovy, or other hybrid languages on the JVM platform (I've came into Java world from PHP/Ruby). Thanks in advance.

    Read the article

  • Why doesn't infinite recursion hit a stack overflow exception in F#?

    - by Amazingant
    I know this is somewhat the reverse of the issue people are having when they ask about a stack overflow issue, but if I create a function and call it as follows, I never receive any errors, and the application simply grinds up a core of my CPU until I force-quit it: let rec recursionTest x = recursionTest x recursionTest 1 Of course I can change this out so it actually does something like this: let rec recursionTest (x: uint64) = recursionTest (x + 1UL) recursionTest 0UL This way I can occasionally put a breakpoint in my code and see the value of x is going up rather quickly, but it still doesn't complain. Does F# not mind infinite recursion?

    Read the article

  • Explain Type Classes in Haskell

    - by Tsubasa Gomamoto
    Hi, I am a C++ / Java programmer and the main paradigm I happen to use in everyday programming is OOP. In some thread I read a comment that Type classes are more intuitive in nature than OOP. Can someone explain the concept of type classes in simple words so that an OOP guy like me can understand it?

    Read the article

  • Avoiding explicit recursion in Haskell

    - by Travis Brown
    The following simple function applies a given monadic function iteratively until it hits a Nothing, at which point it returns the last non-Nothing value. It does what I need, and I understand how it works. lastJustM :: (Monad m) => (a -> m (Maybe a)) -> a -> m a lastJustM g x = g x >>= maybe (return x) (lastJustM g) As part of my self-education in Haskell I'm trying to avoid explicit recursion (or at least understand how to) whenever I can. It seems like there should be a simple non-explicitly recursive solution in this case, but I'm having trouble figuring it out. I don't want something like a monadic version of takeWhile, since it could be expensive to collect all the pre-Nothing values, and I don't care about them anyway. I checked Hoogle for the signature and nothing shows up. The m (Maybe a) bit makes me think a monad transformer might be useful here, but I don't really have the intuitions I'd need to come up with the details (yet). It's probably either embarrassingly easy to do this or embarrassingly easy to see why it can't or shouldn't be done, but this wouldn't be the first time I've used self-embarrassment as a pedagogical strategy. Background: Here's a simplified working example for context: suppose we're interested in random walks in the unit square, but we only care about points of exit. We have the following step function: randomStep :: (Floating a, Ord a, Random a) => a -> (a, a) -> State StdGen (Maybe (a, a)) randomStep s (x, y) = do (a, gen') <- randomR (0, 2 * pi) <$> get put gen' let (x', y') = (x + s * cos a, y + s * sin a) if x' < 0 || x' > 1 || y' < 0 || y' > 1 then return Nothing else return $ Just (x', y') Something like evalState (lastJustM (randomStep 0.01) (0.5, 0.5)) <$> newStdGen will give us a new data point.

    Read the article

  • F# replace ref variable with something fun

    - by Stephen Swensen
    I have the following F# functions which makes use of a ref variable to seed and keep track of a running total, something tells me this isn't in the spirit of fp or even particular clear on its own. I'd like some direction on the clearest (possible fp, but if an imperative approach is clearer I'd be open to that) way to express this in F#. Note that selectItem implements a random weighted selection algorithm. type WeightedItem(id: int, weight: int) = member self.id = id member self.weight = weight let selectItem (items: WeightedItem list) (rand:System.Random) = let totalWeight = List.sumBy (fun (item: WeightedItem) -> item.weight) items let selection = rand.Next(totalWeight) + 1 let runningWeight = ref 0 List.find (fun (item: WeightedItem) -> runningWeight := !runningWeight + item.weight !runningWeight >= selection) items let items = [new WeightedItem(1,100); new WeightedItem(2,50); new WeightedItem(3,25)] let selection = selectItem items (new System.Random())

    Read the article

  • Getting notification / listener when action is performed (chimpChat / monkeyrunner tool)

    - by Dr. AtZe
    I want to get a notification if someone has performed an action in an android app from outside of the app. I don't want to make any (android) code changes. To do the actions I use the Chimpchat.jar, the .jar that the monkeyrunner tool uses. To be clear: Can I get a notification or register listeners on components from outside of the app? e.g. Run my android app My java application links into the device with chimpChat via the adb The user touches a button My java application gets a notification what was performed = am I able to get that information? If not, am I able to get the information on which position the tab was? Hopefully it's clear what I want to do. Thanks, soeren

    Read the article

  • Error in merging two sequences of timestamps to yield strings

    - by AruniRC
    The code sorts two input sequences - seq01 and seq02 - on the basis of their timestamp values and returns a sequence that denotes which sequence is to be read for the values to be in order. For cases where seq02's timestamp value is lesser than seq01's timestamp value we yield a "2" to the sequence being returned, else a "1". These denote whether at that point seq01 is to be taken or seq02 is to be taken for the data to be in order (by timestamp value). let mergeSeq (seq01:seq<_>) (seq02:seq<_>) = seq { use iter01 = seq01.GetEnumerator() use iter02 = seq02.GetEnumerator() while iter01.MoveNext() do let _,_,time01 = iter01.Current let _,_,time02 = iter02.Current while time02 < time01 && iter02.MoveNext() do yield "2" yield "1" } To test it in the FSI created two sequences a and b, a={1;3;5;...} and b={0;2;4;...}. So the expected values for let c = mergeSeq a b would have been {"2","1","2","1"...}. However I am getting this error: error FS0001: The type ''a * 'b * 'c' does not match the type 'int' EDIT After correcting: let mergeSeq (seq01:seq<_>) (seq02:seq<_>) = seq { use iter01 = seq01.GetEnumerator() use iter02 = seq02.GetEnumerator() while iter01.MoveNext() do let time01 = iter01.Current let time02 = iter02.Current while time02 < time01 && iter02.MoveNext() do yield "2" yield "1" } After running this, there's another error: call MoveNext. Somehow the iteration is not being performed.

    Read the article

  • Writing an auto-memoizer in Scheme. Help with macro and a wrapper.

    - by kunjaan
    I am facing a couple of problems while writing an auto-memoizer in Scheme. I have a working memoizer function, which creats a hash table and checks if the value is already computed. If it has been computed before then it returns the value else it calls the function. (define (memoizer fun) (let ((a-table (make-hash))) (?(n) (define false-if-fail (?() #f)) (let ((return-val (hash-ref a-table n false-if-fail))) (if return-val return-val (begin (hash-set! a-table n (fun n)) (hash-ref a-table n))))))) Now I want to create a memoize-wrapper function like this: (define (memoize-wrapper function) (set! function (memoizer function))) And hopefully create a macro called def-memo which defines the function with the memoize-wrapper. eg. the macro could expand to (memoizer (define function-name arguments body ...) or something like that. So that I should be able to do : (def-memo (factorial n) (cond ((= n 1) 1) (else (* n (factorial (- n 1)))))) which should create a memoized version of the factorial instead of the normal slow one. My problem is that the The memoize-wrapper is not working properly, it doesnt call the memoized function but the original function. I have no idea how to write a define inside of the macro. How do I make sure that I can get variable lenght arguments and variable length body? How do I then define the function and wrap it around with the memoizer? Thanks a lot.

    Read the article

  • PHP string to object name.

    - by Smickie
    Ok I have a string... $a_string = "Product"; and I want to use this string in a call to a object like this: $this->$a_string->some_function(); How the dickens do I dynamically call that object? (don't think Im on php 5 mind)

    Read the article

  • How do I use Declarations (type, inline, optimize) in Scheme?

    - by kunjaan
    How do I declare the types of the parameters in order to circumvent type checking? How do I optimize the speed to tell the compiler to run the function as fast as possible like (optimize speed (safety 0))? How do I make an inline function in Scheme? How do I use an unboxed representation of a data object? And finally are any of these important or necessary? Can I depend on my compiler to make these optimizations? thanks, kunjaan.

    Read the article

  • Using items in a list as arguments

    - by Travis Brown
    Suppose I have a function with the following type signature: g :: a -> a -> a -> b I also have a list of as—let's call it xs—that I know will contain at least three items. I'd like to apply g to the first three items of xs. I know I could define a combinator like the following: ($$$) :: (a -> a -> a -> b) -> [a] -> b f $$$ (x:y:z:_) = f x y z Then I could just use g $$$ xs. This makes $$$ a bit like uncurry, but for a function with three arguments of the same type and a list instead of a tuple. Is there a way to do this idiomatically using standard combinators? Or rather, what's the most idiomatic way to do this in Haskell? I thought trying pointfree on a non-infix version of $$$ might give me some idea of where to start, but the output was an abomination with 10 flips, a handful of heads and tails and aps, and 28 parentheses. (NB: I know this isn't a terribly Haskelly thing to do in the first place, but I've come across a couple of situations where it seems like a reasonable solution, especially when using Parsec. I'll certainly accept "don't ever do this in real code" if that's the best answer, but I'd prefer to see some clever trick involving the ((->) r) monad or whatever.)

    Read the article

  • Lambda functions

    - by Oden
    Hey, I'm really interested in the way of using lambda functions. Does it make sense to use them in a high-level programming language? If yes, why? Is this really just a function embedded in a function (like this) or is there more behind?

    Read the article

  • Applying a function to a custom type in F#

    - by Frederik Wordenskjold
    On my journey to learning F#, I've run into a problem I cant solve. I have defined a custom type: type BinTree = | Node of int * BinTree * BinTree | Empty I have made a function which takes a tree, traverses it, and adds the elements it visits to a list, and returns it: let rec inOrder tree = seq{ match tree with | Node (data, left, right) -> yield! inOrder left yield data; yield! inOrder right | Empty -> () } |> Seq.to_list; Now I want to create a function, similar to this, which takes a tree and a function, traverses it and applies a function to each node, then returns the tree: mapInOrder : ('a -> 'b) -> 'a BinTree -> 'b BinTree This seems easy, and it probably is! But I'm not sure how to return the tree. I've tried this: let rec mapInOrder f tree = match tree with | Node(data, left, right) -> mapInOrder f left Node(f(data), left, right) mapInOrder f right | Empty -> () but this returns a unit. I havent worked with custom types before, so I'm probably missing something there!

    Read the article

  • using Python reduce over a list of pairs

    - by user248237
    I'm trying to pair together a bunch of elements in a list to create a final object, in a way that's analogous to making a sum of objects. I'm trying to use a simple variation on reduce where you consider a list of pairs rather than a flat list to do this. I want to do something along the lines of: nums = [1, 2, 3] reduce(lambda x, y: x + y, nums) except I'd like to add additional information to the sum that is specific to each element in the list of numbers nums. For example for each pair (a,b) in the list, run the sum as (a+b): nums = [(1, 0), (2, 5), (3, 10)] reduce(lambda x, y: (x[0]+x[1]) + (y[0]+y[1]), nums) This does not work: >>> reduce(lambda x, y: (x[0]+x[1]) + (y[0]+y[1]), nums) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <lambda> TypeError: 'int' object is unsubscriptable Why does it not work? I know I can encode nums as a flat list - that is not the point - I just want to be able to create a reduce operation that can iterate over a list of pairs, or over two lists of the same length simultaneously and pool information from both lists. thanks.

    Read the article

  • Haskell map function with predicate

    - by Paul
    I feel like this should be fairly obvious, or easy, but I just can't get it. What I want to do is apply a function to a list (using map) but only if a condition is held. Imagine you only wanted to divide the numbers which were even: map (`div` 2) (even) [1,2,3,4] And that would give out [1,1,3,2] since only the even numbers would have the function applied to them. Obviously this doesn't work, but is there a way to make this work without having to write a seperate function that you can give to map? Filter is almost there, except I also want to keep the elements which the condition doesn't hold for, and just not apply the function to them. Thanks

    Read the article

  • Creating Delegates With Lambda Expressions in F#

    - by Matt H
    Why does... type IntDelegate = delegate of int -> unit type ListHelper = static member ApplyDelegate (l : int list) (d : IntDelegate) = l |> List.iter (fun x -> d.Invoke x) ListHelper.ApplyDelegate [1..10] (fun x -> printfn "%d" x) not compile, when: type IntDelegate = delegate of int -> unit type ListHelper = static member ApplyDelegate (l : int list, d : IntDelegate) = l |> List.iter (fun x -> d.Invoke x) ListHelper.ApplyDelegate ([1..10], (fun x -> printfn "%d" x)) does? The only difference that is that in the second one, ApplyDelegate takes its parameters as a tuple. Error 1 This function takes too many arguments, or is used in a context where a function is not expected

    Read the article

< Previous Page | 37 38 39 40 41 42 43 44 45 46 47 48  | Next Page >