Questions tagged [code-translation]

For questions regarding the translation of code from one programming language to another. NOTE that asking to have your code translated is not suitable for Stack Overflow.

Translating code from one programming language to another can be as tricky as translating between natural languages. This is due to the fact that languages implement different sets of features and are often designed with very different goals. What comes as a built-in function in one language may have to be developed from scratch (or at least require a library) to achieve the same functionality in another.

An appropriately formed question might be "How do I translate this statement", or "What are the differences between these two statements in different languages". It is not appropriate to ask for your code to be translated.

388 questions
1
vote
1 answer

Converting from PyTorch to Tensorflow for Self-Attention Pooling Layer

I have found an implementation of the said layer from this paper, "Self-Attention Encoding and Pooling for Speaker Recognition", available at here via Pytorch. However, due to CUDA compatibility issues, I can't want to use the said code. Also, thus…
1
vote
1 answer

Count runs lazily in Python translate from Haskell

I'm trying to write a generator function (or achieve the equivalent) which takes an iterable xs in Python and counts the "runs". (This is a problem in Thinking Functionally with Haskell by Bird, which I want to translate into Python using Python's…
Eric Auld
  • 1,156
  • 2
  • 14
  • 23
1
vote
0 answers

Do-expression expansion in Haskell

I have a fragment of code from the Get Programming with Haskell book which looks as follows: listToSTUArray :: [Int] -> ST s (STUArray s Int Int) listToSTUArray vals = do let end = length vals - 1 myArray <- newArray (0,end) 0 forM_ [0…
1
vote
0 answers

What is the PyQuery equivalent of jQuery's ._data.map()?

I've been tasked to learn to code by translating this program https://github.com/metacoglab/metacognition-task-online from javascript to Python... big undertaking and I only know R so sorry if this is a confused question. Line 30 of the design.js…
M.L.
  • 117
  • 7
1
vote
1 answer

Where does my translation of this code into processing differ from the original?

I have translated the prime testing code from this paper (here is a link to just the original code) into processing. When testing it I found it works for numbers below 10,000,000 but it skips some primes above that. Here is my translation (except…
cbap
  • 51
  • 1
  • 7
1
vote
1 answer

Translation from C# to Delphi

I need help translating part of the C# code to Delphi. I would very much appreciate any help or advice. unsafe { fixed (byte* bpt = &buff[140]) { byte b1 = bpt[0]; byte b2 = bpt[1]; byte b3 = bpt[2]; byte b4 =…
Mark
  • 422
  • 2
  • 10
1
vote
2 answers

Converting from C to Racket Scheme

I have written the following C code to recursively solve for a given problem, which is to take in a non-empty list of integers and a target value, and return the closest positive value without going over the target. e.g. (3 4) with target 2 should…
Giuseppe
  • 63
  • 6
1
vote
4 answers

Idiomatic way to run nested loop with passing value

I want to do something like this int n=0 for(int i=xs; i
Sanghyun Kim
  • 161
  • 1
  • 12
1
vote
3 answers

How to create mutually referencing data structures in Haskell?

I've exploited the fact that when the JVM creates an object (immutable or not), its pointer is created before its fields are initialized. That allows me to create something like this: class BackRefdNode(val…
caeus
  • 3,084
  • 1
  • 22
  • 36
1
vote
2 answers

Python Returning Types Translated to C++

I inherited a bit of code in Python to convert to C++. The Python code throws an exception based on a error code returned by other functions. The Python code uses a utility function called errors(...) that contains creates a dictionary to map error…
Steve
  • 3,957
  • 2
  • 26
  • 50
1
vote
1 answer

Is there a way of processing list of number integers entered via terminal without saving them into list?

I am trying to write a similar code in Python, but I am new to it. int counts[] = { 0, 0, 0, 0, 0 }; for (int i = 0; i < groups; i++) { int groups_size; scanf(" %d", &groups_size); counts[groups_size] += 1; } Please note that it does…
lightsaber
  • 29
  • 6
1
vote
2 answers

Converting expression with >>= to do notation

I have the following code newtype State s a = State { runState :: s -> (s,a) } evalState :: State s a -> s -> a evalState sa s = snd $ runState sa s instance Functor (State s) where fmap f sa = State $ \s -> let (s',a) = runState sa s…
Tomer
  • 1,159
  • 7
  • 15
1
vote
0 answers

Cascading the bind (`>>=`) operator

I have the following function bar :: (Monad m, Num b) => m b -> m b -> m b -> m b bar x y z = do x' <- x y' <- y z' <- z return $ x' + y' + z' and I'm trying to write it without the 'do'…
Tomer
  • 1,159
  • 7
  • 15
1
vote
1 answer

How can I transform this code into Racket/ Scheme

This is the code I want translated into Racket: public static ArrayList convert(int k, int n) { ArrayList lst = new ArrayList<>(); while (k / n != 0) { lst.add(k % n); k = k/n; } …
1
vote
2 answers

Source to Source Translator or Custom Interpreter?

So, I'm wondering if anyone has had any thoughts (hopefully based on experience) on the merits of building a source to source translator vs. building a custom interpreter. I'm working on a project that interprets mathematical functions and so far…
ktr
  • 696
  • 9
  • 15