Questions tagged [imperative-programming]
Imperative programming is a paradigm of expressing the logic of a computer program or computation by explicitly describing its control flow in terms of statements that change a program state.
140
questions
0
votes
1
answer
34
views
Functional vs imperative (statements): writing this in the most compact way
Looking for the shortest, most elegant wat to write this. I really like option 2. However, i get this error:
Argument of type 'boolean[]' is not assignable to parameter of type 'SetStateAction<...
1
vote
1
answer
103
views
Incrementing a counter with functional programming vs imperative programming
I likely am asking a very stupid question here, please forgive me.
I am a Java and C# backend-engineer with relatively good knowledge of OOP design patterns. I have recently discovered the debate ...
1
vote
2
answers
47
views
sort a singly linked list formed of 1, 2 and 0s by swapping the nodes through one traversal in C
I'm trying to solve the question that I need to sort a given linked list which values consist of 1, 2 and 0 by traversing the list only once and I need to swap the nodes not values.
my code is:
#...
-1
votes
1
answer
76
views
OCaml loops: imperative vs recursion [closed]
I just stumbled upon some OCaml code that writes a loop like this:
let r = ref (f 0) in
for i = 1 to k - 1 do
r := f i * !r
done ;
!r
in
Which is interesting as I normally see ...
3
votes
1
answer
104
views
Updating an outer variable in Haskell
There is a sense in which Haskell is a purely functional language, and certainly, idiomatic code tries to be as functional as reasonably possible. At the same time, Haskell does provide support for ...
0
votes
0
answers
83
views
Advantages of Functional Programming with Java 8 Stream API
Soon I want to show advantages of functional programming to some people. I plan to do this via Java 8 Stream API. I wanted to show that imperative programming can lead to side effects.
I've built a ...
1
vote
1
answer
95
views
gotoxy in game condign with C
I am currently developing a Duck Hunt-style game with C language for a university class, but I'm having a problem that I can't move the player's aim while keeping the targets moving, for some reason, ...
2
votes
2
answers
225
views
Functional programming: Declarative vs imperative
Functional programming insists on telling what to do, rather than how to do.
For example,Scala's collections library has methods like filter, map etc. These methods enable developers to get rid of ...
0
votes
2
answers
50
views
Why do I get a type unit when I want to concatenate the element of the array with an empty list?
I am trying to make a list out of a given array and in my code it returns an empty list.
I want to know why this is impossible to do with list. Is it because we are supposed to return something in the ...
1
vote
1
answer
52
views
Functional approach to storing element counts in Map
I came across a problem where I needed to count the number of Strings in an Iterable and store the sums in a Map. I came up with the following imperative solution:
private static Map<String, ...
0
votes
1
answer
323
views
How can I generate imperative non-OOP python code from UML/SysML model?
I want to generate python code based on UML/SysML. The coding is mainly imperative - calling functions that deliver results, switch states and some main part.
How can I create some simple main program,...
-4
votes
1
answer
78
views
Why is functional-styled code so much faster than imperative code in C?
I've been working on an algorithm for calculating maximum depth of an expression (i.e. how many nested parentheses there are) in various languages just for fun/practice.
I noticed there's a huge ...
1
vote
1
answer
406
views
Spring Reactive implementation calling db multiple times
I a new to reactive and trying to accomplish the below task in efficient way. I have a table having events for each user. I am trying to get latest row of each event name for a given user filtered ...
-1
votes
6
answers
812
views
Finding the sum of numbers from x to y and the program prints the answer as "x+(x+1)....+y= (sum of # from x to y)" [closed]
For example, the sum of numbers from 1 to 3 would be printed as 1+2+3=6; the program prints the answer along with the numbers being added together. How would one do this? Any help is greatly ...
2
votes
4
answers
139
views
Better way to express "if false return false" construct
In many cases you have code like this (C-style pseudo code used):
bool checkCondition();
bool doSomething(){
if (checkCondition() == false)
return false;
// do something
return true;
...
2
votes
1
answer
128
views
Why re-constructing view hierarchy is affordable in declarative UI frameworks like Flutter? [closed]
I am reading Flutter's official documentation on state management and in this page it says:
For example, in Flutter it’s okay to rebuild parts of your UI from scratch instead of modifying it. ...
2
votes
1
answer
229
views
Is it common to have code that looks "imperative" supporting reactive code?
It seems like I'm starting to write "imperative looking" methods to support the functionality/readability of the reactive chains. This may not be the most technical of questions, but is this common?
@...
50
votes
2
answers
3k
views
What are the performance impacts of 'functional' Rust?
I am following the Rust track on Exercism.io. I have a fair amount of C/C++ experience. I like the 'functional' elements of Rust but I'm concerned about the relative performance.
I solved the 'run ...
0
votes
2
answers
2k
views
Using ternary operator just for the side effect - Good or Bad?
Suppose we are using AVKit and we have added a target to our play/pause button to handle the playing/pausing of a track. Is it generally bad practice to use a ternary operator just for its side effect,...
2
votes
1
answer
691
views
How do I tell tell a child element in React to seek its video element?
I have a simple React app with a video player and chart displaying data about the video. Both are in their own components at the top level:
class App extends Component {
...
render() {
return ...
0
votes
3
answers
89
views
Why functional style search acts faster, then imperative in Java
I’m trying to figure out , why compiler looks through the collection using functional style swiftly then imperative. Here is the simple example where functional search goes first and takes 9.5 sec to ...
2
votes
1
answer
110
views
Converting iteration loop body into functional code Java 8
I want to convert the below imperative code into functional code using Java 8.
The way it works is I capture the current time before and after the API call, then I subtract the before from the after....
-1
votes
2
answers
502
views
Mutable and Immutable objects are implemented the same in all programming languages?
The main ideas of immutability are kept the same throughout the scope of OOP and functional programming or do, for example, Java and Python have their own versions of immutability. More specifically ...
0
votes
1
answer
176
views
Link between models of computation, computer system architectures and programming paradigms
I have been reading about these topics for a while and may have understood something. But I am confused with some connections:
i. Turing Machine (RAM model to be exact) & Imperative Programming
...
1
vote
1
answer
154
views
How are data structures created in declarative programming?
An article I read gave this example for the difference between declarative and imperative programming:
Declarative
small_nums = [x for x in range(20) if x < 5]
Imperative
small_nums = []
for i ...
0
votes
2
answers
945
views
How to modify last element of a generator in python?
I have a generator and I want to modify the last element of the generator.
I want to replace the last element with another element. I know how to retrieve the last element, but not how to modify it.
...
1
vote
2
answers
205
views
Underscore.js - Imperative & Functional exercise
I'm learning about Underscore.js and the higher order functions. I came across the following problem, and I'm just simply stuck.
I'm not looking for a handout but at the same time, I don't have ...
3
votes
1
answer
456
views
OCaml variable, which keeps it value between function calls
Is there a way in OCaml for a variable inside a function to keep its value between function calls? It should work like Pythons default argument, which is a reference to the same object in every ...
15
votes
4
answers
9k
views
Reason for using reactive programming in simple cases
Please, can somebody explain me what are the advantages of using reactive style:
Observable<String> greeting = Observable.just("Hello");
Observable<String> yelling = greeting.map(s -> ...
2
votes
3
answers
2k
views
large amount of if else refactor
There is a if/else statement in my code
and Im thinking of refactoring it.I have already searched many Similar Questions.like this.
The best answer said that the chain-of-responsibility pattern is a ...
0
votes
3
answers
605
views
Pointer to a record in OCaml
I am implementing binary search trees in OCaml, trying to use as much imperative programming as possible.
I have the following data type:
type tKey = Key of int;;
type tBST = Null | Pos of node ref
...
-1
votes
2
answers
120
views
Is unix's 'cp' command declarative or imperative? [closed]
I'm having a discussion with a coworker, who insists cp is declarative. To me, it seems very imperative. It's an instruction we are asking the computer to do, not a definition or declaration. For it ...
1
vote
0
answers
94
views
Haskell MArray performance benchmark
Haskell's MArray is supposed to provide a mutable array.
I wonder how does it compare with a normal mutable array in an imperative programming language like Java or C. Is there any performance ...
2
votes
1
answer
279
views
Interning strings in declarative programming
The following scenario shows an abstraction that seems to me to be impossible to implement declaratively.
Suppose that I want to create a Symbol object which allows you to create objects with strings ...
1
vote
5
answers
19k
views
The length of a list without the "length" function in Haskell
I want to see how long a list is, but without using the function length. I wrote this program and it does not work. Maybe you can tell me why? Thanks!
let y = 0
main = do
list (x:xs) = list (xs)
...
3
votes
1
answer
653
views
Imperative vs Functional - understanding the von Neumann bottleneck
In the Coursera course - Functional Programming in Scala - Martin Odersky talks about how Imperative Programming is constrained by the von Neumann bottleneck because it deals largely with mutable ...
11
votes
1
answer
163
views
Fast impertive pointers (static, unboxing, etc.) with Struct library
I am interested in using more efficient pointers for a project implementing an imperative language in Haskell. There is already a library for that: Struct. There is a blog post on it and brief ...
-2
votes
2
answers
1k
views
Is Haskell an imperative or declarative paradigm?
I have read some sources where the Haskell's paradigm is described as functional but imperative paradigm. The main source where this is said is Wikipedia. How is possible a functional and imperative ...
-1
votes
2
answers
595
views
Functional Programming vs Imperative Programming: Computation Time
I've got a general question about functional programming vs imperative programming. I am a hobby programmer implementing some engineering problems with C#. You always here a lot concerning the ...
-10
votes
1
answer
1k
views
C++ Part of brute-force knapsack
reader,
Well, I think I just got brainfucked a bit.
I'm implementing knapsack, and I thought about I implemented brute-force algorithm like 1 or 2 times ever. So I decided to make another one.
And ...
0
votes
0
answers
234
views
Can pure imperative programming incorporate functions
As the question suggests, if a program should be written in a purely imperative style (even if the programming language is an amalgamated language like PHP or Python which support other paradigms like ...
0
votes
1
answer
59
views
Are data races intrinsic to imperative programming and an obstacle for parallel computing?
I don't study this area of computing to be honest. Actually my references is some web and academic articles then I'm insecure but curious about some concepts of parallel computing.
I've formulated ...
1
vote
1
answer
98
views
Immutable state is a functional feature in ReactJs?
The most important feature of an imperative program is the state and their modifications.
ReactJs encourages as much functional style of programming (e.g. using purity, higher-order functions). I ...
4
votes
2
answers
234
views
SICP - Imperative versus Functional implementation of factorial
I am studying the SICP book with Racket and Dr. Racket. I am also watching the lectures on:
https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-001-structure-and-interpretation-...
0
votes
1
answer
309
views
Better readability and simplicity versus higher complicity and speed in programming, what to chose? [closed]
Solving my homework for Informatics at University, I always find different ways to solve the same problem.
One way is the fastest(execution time) but the longest and more complex.
And the other is ...
7
votes
3
answers
448
views
Why functional programming language support automated memoization but not imperative languages?
This is a question I read on some lectures about dynamic programming I randomly found on the internet. (I am graduated and I know the basic of dynamic programming already)
In the section of ...
0
votes
2
answers
89
views
Possible OCaml code generation bug
The following self contained code highlights a problem in OCaml, possibly with the code generation.
Array x has connectivity information for nodes in [0..9]. Function init_graph originally constructed ...
2
votes
0
answers
211
views
Imperative vs. Functional data structure tradeoffs
Is there some trade-off between these two examples of the same thing?
My bias is to always use a functional style, but if I'm running billions of these foldLefts, is creating a new FunctionalA for ...
-1
votes
2
answers
68
views
C and addition, integer first and later [duplicate]
I'm come from Java, i want to improve my skill in coding and knowledge of how it's work in deep and i figure that the best language for this is C the mother of all. I'm very excited about how it work ...
0
votes
1
answer
28
views
What does it mean to convert a function to a table look up?
In this video titled Don't fear the monad, between 05:02 and 06:05, Brian Beckman says:
Every imperative programmer goes through this phase of learning that
functions can be replaced with table ...