Optimization of a "Sieve of Eratosthenes" in Java I have created a simple Pseudo "Sieve of Eratosthenes," for a class in Java. I am looking for the most optimized option, or at least an option that is more optimized than current. If there is any other advice that you have to offer, I would be more than happy to hear that as well.

import java.util.*;

public class SieveOfErantosthenes {

    public static void main(String[] args) {
        int testerIndex = 0, numbersOnLine = 0;
        Vector<Integer> primes = new Vector<Integer>(1, 1);
        for (int i = 0; i < 1000; i++) {
            primes.add(i + 1);
        }
        primes.remove(0);
        while ((primes.get(testerIndex)) < 500) {
            for (int i = testerIndex + 1; i < 999; i++) {
                if (primes.get(i) % primes.get(testerIndex) == 0
                        && primes.get(i) != 0) {
                    primes.set(i, 0);
                }

            }
            do {
                testerIndex++;
            } while (primes.get(testerIndex) == 0);
        }
        while (primes.contains(0)) {
            primes.remove((Integer) 0);
        }
        testerIndex = 0;
        while(testerIndex < primes.size()){
            for(; numbersOnLine < 15 && testerIndex < primes.size(); testerIndex++, numbersOnLine++){
                System.out.print(primes.get(testerIndex) + "\t");
            }
            System.out.println();
            numbersOnLine = 0;
        }
    }

}

Alright, a few notes:

Your for loop strikes one as odd.

 for (int i = 0; i < 1000; i++) {
                primes.add(i + 1);
            }

Why not start iteration at 1 and have your conditional be  <= 1000, and you could just call primes.add(i) Note that this is just an example to be equivalent to yours, just removing unnecessary bloat, it actually makes the most sense to start with 2 for a sieve, since it's the first prime. These changes would more clearly communicate your intention.

Courtesy of this answer I learned that a sieve is best implemented through a simple boolean array.

  boolean[] sieve = new boolean[1000];
    Arrays.fill(sieve, true);

 for (int prime = 2; prime < sieve.length; prime++) {
        if (sieve[prime]) {
            // this is a prime number
          for (int notPrime = prime * 2; notPrime < sieve.length; notPrime += prime) {
              sieve[notPrime] = false;
          }
        }
    }

As this answer notes there may be ways to improve it, but I like this implementation for being light on memory (booleans are 1 bit) and the performance and convenience of simply using the indexes as the prime or not prime values.

Edit: Although booleans actually take up 1 bit in memory, the actual number of bits allocated is JVM dependent and typically a byte is reserved for each boolean used. Although the performance would be intangible using a Bitset would be the target route to achieve 1 bit per boolean.

*If any of this was useful, please give the linked answer up-vote priority.


Sum of primes in given range in Go This piece of code calculates the sum of all prime numbers below 1 million.

It stood out to me that the Ruby script is around 20% faster. I know that Go should be faster just for being a compiled language. So how can I make the Go code run faster than it already does?

I'm interested in solutions involving goroutines, pointers, both, etc.

In Go:

Execution time: ~4.6s

package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := time.Now()

    // Create slice containing values from 0 to 1 million
    a := []int{}
    for i := 0; i < 1000000; i++ {
        a = append(a, i)
    }

    done := false
    factor := 1
    for !done {
        // Get the next factor to be used as a filter for slice `a`
        factor = nextPrime(a, factor)

        var modified bool
        // Filtering slice `a`
        for i, v := range a {
            if v%factor == 0 && v > factor {
                a[i] = 0 // This is equivalent to eliminating the value
                modified = true
            }
        }

        if !modified {
            done = true
        }
    }

    var sum int
    for _, v := range a {
        sum += v
    }
    fmt.Println(sum - 1)

    t2 := time.Now()
    fmt.Println("Execution duration:", t2.Sub(t1))
}

// Doesn't return a prime literaly, but the least value to be used
// to filter out non prime numbers from an array or slice
func nextPrime(a []int, factor int) (newFactor int) {
    for _, v := range a {
        if v > factor {
            newFactor = v
            break
        }
    }
    return
}

In Ruby:

Execution time: ~3.5s

t1 = Time.now

def next_prime(a, t)
  a.each { |m| return m if m > t }
end

a=(2..999_999).to_a

complete = false

until complete
  t ||= 1
  t = next_prime(a, t)
  presize = a.size
  a.map! { |m| m%t == 0 && m > t ? nil : m }
  a.compact!
  complete = true if a.size == presize # when factoring stops shortening the array, we're done
end

r = 0
a.each { |m| r+=m }
puts r

t2 = Time.now
puts t2 - t1

I don't know Ruby, and cannot infer how similar are the algorithms (if they are significantly different, there is no sense to compare performance). However I can point some performance problems in the go code.

The range loop

    for i, v := range a {
        if v%factor == 0 && v > factor {
            a[i] = 0
            modified = true
        }
    }

does a lot of unnecessary tests. A (more controlled) for loop seems to suite better:

    for i := factor*factor; i < 10000000; i += factor {
        a[i] = 0
    }

The search for next prime starts from the very beginning of a - rendering the search effectively quadratic. There is no need to search an initial [0, factor] slice; start with factor + 1:

    for _, v := range a[factor + 1:] {


JavaScript module to render and handle a form to add users I recently switched to modular JavaScript and really like the idea of having the state of your application in JavaScript and not in the DOM. I want to know if what I am doing is considered best practices or not and how I can avoid re-rendering the entire list after each change.

First, the code:

HTML

<form id="add-user-form">
  <input type="text" placeholder="username" name="username">
  <button>add User</button>
</form>

JavaScript

// lets you get templates from HTML ( as you can see in the HTML above ).
// This is to avoid writing out HTML inside JavaScript
const Template = {
  cached: {},
  get(templateId, placeholders) {
   let templateHtml = ''
   if (!this.cached[templateId]) {
      templateHtml = document.getElementById(templateId).innerHTML.trim()
      this.cached[templateId] = templateHtml
   }
   else {
      templateHtml = this.cached[templateId]
   }

   for (key in placeholders) {
     templateHtml = templateHtml.replace(new RegExp("\\${\\s*" + key + "\\s*}", "g"), placeholders[key]);
   }
   return templateHtml
  }
}

const Users = {
  users: [],
  init() {
    this.cacheDom()
    this.bindEvents()
  },
  cacheDom() {
    this.addUserFormEl = document.getElementById('add-user-form')
    this.userListEl = document.getElementById('user-list')
  },
  bindEvents() {
    this.addUserFormEl.addEventListener('submit', this.handleAddUser.bind(this))
  },
  render() {
    // gets the HTML from ALL users of our user list and replaces the current user list
    const userHtml = this.users.map( user => Template.get('user-template', user) ).join('')
    this.userListEl.innerHTML = userHtml
  },
  handleAddUser(e) {
    e.preventDefault()

    const username = e.currentTarget.elements['username'].value
    if (!username) return
    e.currentTarget.elements['username'].value = ''

    const key = this.users.length // I know I know this is bad practice, please ignore :)

    this.users.push({
      key: key,
      name: username
    })

    this.render()
  }
}

Users.init()

The code has a form with an input field where you can type in a name that, once the form gets submitted, will be added to the user list below. It would probably be a good idea to separate the userlist and the userform into seperate concerns, but for the sake of this example I kept it simple.

I want to keep DOM modifications to a minimum so I put them inside the method Users.render. This however has one side-effect. Whenever I add an user, it reloads the entire list. It might work with this amount of data, but let's say I have thousands of records, maybe even with input fields. These would all be emptied again.

I could of course just add the new entry to the DOM inside the Users.handleAddUser method but what if later I also want to delete users? I would have to delete the entry within Users.handleRemoveUser and would then have two methods that handle DOM modifications for the same thing.

I know this all can be easily and beautifully achieved with frameworks such as VueJs that use a vDOM but I am looking for a vanillaJs way of handling this.
(Please note that the code is just an example. Yes Unit testing and typeScript are missing and I am well aware of the fact that the code will not run in browsers that do not support ES-6).

Your Concerns/Questions

  Whenever I add an user, it reloads the entire list.

One alternate approach would be to have the handleAddUser() store the newly added user in a property (e.g. recentlyAdded) and then have the render() method look for that property - if it is set (to something other than null) then add the rendered template of the new user and clear that property.

When the page loads, is there an existing list of users that gets added to the list? If so, maybe those could be stored in a different property and the existing code in render() could look for that property for rendering the existing records.

  I could of course just add the new entry to the DOM inside the Users.handleAddUser method but what if later, I also want to delete users.  I would have to delete the entry within Users.handleRemoveUser and would so already have two methods that handle DOM modifications for the same thing.

Going along with the alternate approach above, one could have the handleRemoveUser method also store the key of the recently removed user, and then the render() method can look for that property too and remove such an element associated with that key - perhaps by adding an id or other data attribute to the template.

You didn't include an implementation for handleRemoveUser so I can only guess as to what it would be: some way of removing the user from the list this.user. If this.users is still an array, then looking for the user to remove might require a loop. However if this.users is an associative-array (i.e. object with keys corresponding to the key of each user) (or a Map), then looking for the user to remove can be achieved without a loop.

See this demonstrated in the snippet below.

In revision 2 you had this question, altered in revision 5:

  As you can see I am not getting the input by ID but by form.elements and then referencing the name.

It is fine to do that, but because the form data doesn't get sent to the server-side, you could also use the id attribute on the input instead of the name attribute, and then fetch that element by id.

Other Feedback

The Template.get() function loops over the keys in placeholders:

for (key in placeholders) {

For this loop, key is a global variable, which may or not be intentional. In general it is best to avoid global variables. One reason would be that if code in a separate function makes its own variable with the same name, there would be no chance of overwriting the value if it isn't a global variable. Use const to declare key as local to that loop.

for (const key in placeholders) {

I recently saw this SO answer that offers a solution to interpolating a string literal similar to how a template literal would be interpolated. I compared it with the regular expression approach in your code and found it to be much slower (see this jsPerf test). I was hoping I could offer that suggestion but unfortunately it can be much slower.

// lets you get templates from HTML ( as you can see in the HTML above ).
// This is to avoid writing out HTML inside JavaScript
const Template = {
  cached: {},
  get(templateId, placeholders) {
   let templateHtml = ''
   if (!this.cached[templateId]) {
      templateHtml = document.getElementById(templateId).innerHTML.trim()
      this.cached[templateId] = templateHtml
   }
   else {
      templateHtml = this.cached[templateId]
   }

   for (let key in placeholders) {
     templateHtml = templateHtml.replace(new RegExp("\\${\\s*" + key + "\\s*}", "g"), placeholders[key]);
   }
   return templateHtml
  }
}

const Users = {
  users: {},
  recentlyAdded: null,
  recentlyRemovedKey: null,
  nextKey: 1,
  init() {
    this.cacheDom()
    this.bindEvents()
  },
  cacheDom() {
    this.addUserFormEl = document.getElementById('add-user-form')
    this.userListEl = document.getElementById('user-list')
  },
  bindEvents() {
    this.addUserFormEl.addEventListener('submit', this.handleAddUser.bind(this))
    this.userListEl.addEventListener('click', this.handleListClick.bind(this));
  },
  render() {
    // gets the HTML from ALL users of our user list and replaces the current user list
    /*const userHtml = this.users.map( user => Template.get('user-template', user) ).join('')
    this.userListEl.innerHTML = userHtml*/
    if (this.recentlyAdded) {
     this.userListEl.innerHTML += Template.get('user-template', this.recentlyAdded);
     this.recentlyAdded = null;
    }
    if (this.recentlyRemovedKey) {
     const element = document.getElementById(this.recentlyRemovedKey);
      if (element) {
       element.remove();
      }
     this.recentlyRemoved = null;
    }
    console.log('userlist after render: ',this.users);
  },
  handleAddUser(e) {
    e.preventDefault()

    const username = e.currentTarget.elements['username'].value
    if (!username) return
    e.currentTarget.elements['username'].value = ''

    const key = this.nextKey++;

    this.users[key] = {
      key: key,
      name: username
    };
    this.recentlyAdded = this.users[key];

    this.render();
  },
  handleListClick(e) {
    const target = e.target;
   if (target.classList.contains('remove')) {
     this.handleRemoveUser(target.parentNode.id);
    }
  },
  handleRemoveUser(key) {
   delete this.users[key];
    this.recentlyRemovedKey = key;
    this.render();
  }
}

Users.init()
.remove:before {
  content: 'X';
}
<form id="add-user-form">
  <input type="text" placeholder="username" name="username">
  <button>add User</button>
</form>


What is the simplest way to hide labels and inputs with javascript? What is the simplest way of hiding labels and inputs in a way that they do not affect the layout. See image and the code below. The label text3 is hidden and there is no extra gap between text2 and text4. In this implementation I put both in div container and change its .style.display property.

Edit: the figure is excactly what I want but the implementation is not good. In other words, how to rewrite the function (e.g. getting rid of div and using css...).

<html>
<body>
<script type="application/javascript">

    function hideableLabelAndTextInput(divId, inputId, labelTxt){
        // container
        var hiderDiv = document.createElement('div');
        hiderDiv.style.display = 'block';
        hiderDiv.id = divId

        // label
        var newLabel = document.createElement('label');
        newLabel.innerHTML = labelTxt;

        // textBox
        var newTextBox = document.createElement('input');
        newTextBox.type = 'text';
        newTextBox.id = 'inputId';

        // grouping
        hiderDiv.appendChild(newLabel);
        hiderDiv.appendChild(newTextBox);

        return hiderDiv;
    }

    for (var i=0; i<10; i++){
        elem = hideableLabelAndTextInput('div' + i, 'text' + i, 'text' + i);
        document.body.appendChild(elem);
    }

    document.getElementById('div3').style.display = 'none';
    document.getElementById('div6').style.display = 'none';

</script>
</body>
</html>

I would use a combination of CSS and JavaScript for this use-case. Essentially what you want to achieve is a simple solution to showing and hiding form elements.

This is what I would suggest for markup:

<form id="myform" method="POST" action="/some/processing/script">
    <fieldset id="group-a" class="input-group">

            My Element Label
            <input type="text" id="my-input-element-1" value="" />

    </fieldset>
</form>

CSS:

#myform label {
    display: block;
    margin: 0 0 10px 0; /* Set the bottom margin to whatever you want */
}

#myform label > span {
    display: block;    /* display:block will force the input element below it */
    margin: 0 0 3px 0; /* Set the bottom margin to whatever you want */
}

#myform label > input[type="text"] {
    display: block;    /* The default is inline, but we want to span the whole width */
    width: 100%; /* Full width */
    padding: 5px 0; /* Trick to allow an input to span the full width, without poking out of the containing element */
    margin: 0; /* Set the bottom margin to whatever you want */
    text-indent: 5px; /* Indent the text to make it appear normally */
}

#myform label.hidden {
    display: none !important;
    visibility: hidden !important;
}

/* OR */

.hidden {
  display: none !important;
  visibility: hidden !important; /* Prevent element from affecting the box model, e.g. whitespace between visible, surrounding elements  */
}

Now, with this setup in mind, you should only have to add or remove the class .hidden from either the containing  element or apply it to the <fieldset /> element if you want to hide the whole group.

Also, if you're going to go to the trouble of hiding input fields, I'd recommend setting the disabled attribute, e.g. <input type="text" disabled="disabled" /> to prevent it's values from being serialized/submitted as part of the form.


AVX SIMD in matrix multiplication I have coded the following C function for multiplying two NxN matrices and using AVX vectors to speed up the calculation. It works but the speedup is not what is to be expected(some scalar code is faster).

What are my major mistakes when trying to use AVX? (I'm new to programming with it so I don't fully understand how the pitfalls when programming for performance)

void version5(int mat1[N][N], int mat2[N][N], int result[N][N])
{
__m256i vec_multi = _mm256_setzero_si256(); //Initialize vector to zero
__m256i vec_mat1 = _mm256_setzero_si256(); //Initialize vector to zero
__m256i vec_mat2 = _mm256_setzero_si256(); //Initialize vector to zero

int i, j, k;
for (i = 0; i < N; i += 8)
{
    for (j = 0; j < N; ++j)
    {
        //The cost heavy storing is done once for each iteration and then use them at all places needed, stores column of matrix2 in chunks of eight, uses setr to have them in the right order
        vec_mat2 = _mm256_setr_epi32(mat2[i][j], mat2[i+1][j],mat2[i+2][j],mat2[i+3][j], mat2[i+4][j],mat2[i+5][j],mat2[i+6][j],mat2[i+7][j]);

        for (k = 0; k < N; ++k)
        {
            vec_mat1 = _mm256_loadu_si256(&mat1[k][i]); //Stores row of first matrix (eight in each iteration)
            vec_multi = _mm256_add_epi32(vec_multi ,_mm256_mullo_epi32(vec_mat1, vec_mat2));//Multiplies the vectors

            result[k][j] += _mm256_extract_epi32(vec_multi, 0) + _mm256_extract_epi32(vec_multi, 1) +_mm256_extract_epi32(vec_multi, 2) +_mm256_extract_epi32(vec_multi, 3) +_mm256_extract_epi32(vec_multi, 4) +_mm256_extract_epi32(vec_multi, 5) +_mm256_extract_epi32(vec_multi, 6) +_mm256_extract_epi32(vec_multi, 7);

            vec_multi = _mm256_setzero_si256();
        }
    }
}
}

The biggest performance problem (for small matrices), and a common mistake, is doing this:

_mm256_extract_epi32(vec_multi, 0) + _mm256_extract_epi32(vec_multi, 1) +_mm256_extract_epi32(vec_multi, 2) +_mm256_extract_epi32(vec_multi, 3) +_mm256_extract_epi32(vec_multi, 4) +_mm256_extract_epi32(vec_multi, 5) +_mm256_extract_epi32(vec_multi, 6) +_mm256_extract_epi32(vec_multi, 7);

This dominates not just horizontal screen space, but also the performance (or lack thereof) of the loop.

Actual extracts (an extract with an index of 0 is turned into vmovd by a reasonable compiler, which is less of a problem) have a throughput of only 1 per cycle on typical CPUs. There are 7 here, so even with just this horizontal addition the loop body could only execute once every 7 cycles (but there is other stuff in there too so it's worse).

There are slightly faster ways to do horizontal sums, for example (not really tuned or anything, just some simple "better than totally naive" hsum)

int hsum_epi32(__m256i x)
{
    __m128i l = _mm256_extracti128_si256(x, 0); //implicit
    __m128i h = _mm256_extracti128_si256(x, 1);
    l = _mm_add_epi32(l, h);
    l = _mm_hadd_epi32(l, l);
    return _mm_extract_epi32(l, 0) + _mm_extract_epi32(l, 1);
}

But that is not great either. Ideally there should just be no horizontal operation in the inner loop. Even more ideally, not anywhere. And that can be arranged: a block of 8 results can be computed by rearranging the computation so that horizontal operations turn into broadcasts. Broadcasting from memory is pretty cheap (naturally it sort of "wastes" the load by loading only one thing, but it's not a slow operation), and there are much fewer of them, so that's probably better.

Sort of like this (to show the general idea, not tested)

for (int i = 0; i < N; i++) {
  for (int j = 0; j < N; j += 8) {
    auto sum = _mm256_setzero_si256();
    for (int k = 0; k < N; k++) {
      auto bc_mat1 = _mm256_set1_epi32(mat1[i][k]);
      auto vec_mat2 = _mm256_loadu_si256((__m256i*)&mat2[k][j]);
      auto prod = _mm256_mullo_epi32(bc_mat1, vec_mat2);
      sum = _mm256_add_epi32(sum, prod);
    }
    _mm256_storeu_si256((__m256i*)&result[i][j], sum);
  }
}

There is some room for improvement. The memory access pattern through mat2 is not ideal, the inner loop iterates over the rows and only half of a cache line is used every time, for a large enough matrix that means half of every cache miss is wasted. It's probably better to unroll the middle loop by 2 (or 4) again. As a bonus, we get to re-use the broadcast from mat1. Sort of like this (not tested, showing 2x)

for (int i = 0; i < N; i++) {
  for (int j = 0; j < N; j += 16) {
    auto sumA = _mm256_setzero_si256();
    auto sumB = _mm256_setzero_si256();
    for (int k = 0; k < N; k++) {
      auto bc_mat1 = _mm256_set1_epi32(mat1[i][k]);
      auto vecA_mat2 = _mm256_loadu_si256((__m256i*)&mat2[k][j]);
      auto vecB_mat2 = _mm256_loadu_si256((__m256i*)&mat2[k][j + 8]);
      auto prodA = _mm256_mullo_epi32(bc_mat1, vecA_mat2);
      auto prodB = _mm256_mullo_epi32(bc_mat1, vecB_mat2);
      sumA = _mm256_add_epi32(sumA, prodA);
      sumB = _mm256_add_epi32(sumB, prodB);
    }
    _mm256_storeu_si256((__m256i*)&result[i][j], sumA);
    _mm256_storeu_si256((__m256i*)&result[i][j + 8], sumB);
  }
}

But, it's probably still possible to improve it, and I didn't test anything.

I've done some testing now, and made some improvement specifically for larger matrices (below N=64 it doesn't really help).

Some results on Haswell, compiled with MSVC[1] 2017, measuring the time (in cycles) per element of the result matrix (so you can mentally compare it to how much time it should take). Time results were eyeballed and rounded to a "typical" value.

       64 128  256  512  1024  2048
naive  90 305 1360 2700  9500   TLE
v1     19  45  170  340  1460  7900
v2     18  40  170  245  1030  4300
v3     22  44   85  150   380   950
v4     17  35   70  150   310   750
v5     18  35   70  140   275   550
ideal  16  32   64  128   256   512

ideal is based on the throughput of vpmulld on Haswell, which is one every two cycles. So per cycle there can be 4 multiplications, we need N of them, so N/4 is the ideal time per element. For small sizes that's not so hard to get near, but for bigger matrices the memory access pattern messes up everything. v1 and v2 are the versions from above.

v3 adds loop tiling to significantly improve the performance for bigger matrices. Understandably this causes some overhead, noticeable for smaller matrices. v4 unrolls the i loop by 2x. Annoyingly, the best choice for the block size depends not only on cache size, but also on the size of the matrix. The times above are not all with the same parameters, but tuned a bit per N.

I'm not sure where to go from here but it seems as though some improvement should still be possible, for bigger sizes it's still a decent factor away from the ideal.

v3:

size_t jb = std::min(512u, N);
size_t kb = std::min(24u, N);

for (size_t jj = 0; jj < N; jj += jb)
{
    for (size_t kk = 0; kk < N; kk += kb)
    {
        for (size_t i = 0; i < N; i += 1) {
            for (size_t j = jj; j < jj + jb; j += 16) {
                __m256i sumA_1, sumB_1;
                if (kk == 0) {
                    sumA_1 = sumB_1 = _mm256_setzero_si256();
                }
                else {
                    sumA_1 = _mm256_load_si256((__m256i*)&result[i][j]);
                    sumB_1 = _mm256_load_si256((__m256i*)&result[i][j + 8]);
                }
                size_t limit = std::min(N, kk + kb);
                for (size_t k = kk; k < limit; k++) {
                    auto bc_mat1_1 = _mm256_set1_epi32(mat1[i][k]);
                    auto vecA_mat2 = _mm256_loadu_si256((__m256i*)&mat2[k][j]);
                    auto vecB_mat2 = _mm256_loadu_si256((__m256i*)&mat2[k][j + 8]);
                    sumA_1 = _mm256_add_epi32(sumA_1, _mm256_mullo_epi32(bc_mat1_1, vecA_mat2));
                    sumB_1 = _mm256_add_epi32(sumB_1, _mm256_mullo_epi32(bc_mat1_1, vecB_mat2));
                }
                _mm256_storeu_si256((__m256i*)&result[i][j], sumA_1);
                _mm256_storeu_si256((__m256i*)&result[i][j + 8], sumB_1);
            }
        }
    }
}

v4:

size_t jb = std::min(512u, N);
size_t kb = std::min(24u, N);

for (size_t jj = 0; jj < N; jj += jb)
{
    for (size_t kk = 0; kk < N; kk += kb)
    {
        for (size_t i = 0; i < N; i += 2) {
            for (size_t j = jj; j < jj + jb; j += 16) {
                __m256i sumA_1, sumB_1, sumA_2, sumB_2;
                if (kk == 0) {
                    sumA_1 = sumB_1 = sumA_2 = sumB_2 = _mm256_setzero_si256();
                }
                else {
                    sumA_1 = _mm256_load_si256((__m256i*)&result[i][j]);
                    sumB_1 = _mm256_load_si256((__m256i*)&result[i][j + 8]);
                    sumA_2 = _mm256_load_si256((__m256i*)&result[i + 1][j]);
                    sumB_2 = _mm256_load_si256((__m256i*)&result[i + 1][j + 8]);
                }
                size_t limit = std::min(N, kk + kb);
                for (size_t k = kk; k < limit; k++) {
                    auto bc_mat1_1 = _mm256_set1_epi32(mat1[i][k]);
                    auto vecA_mat2 = _mm256_loadu_si256((__m256i*)&mat2[k][j]);
                    auto vecB_mat2 = _mm256_loadu_si256((__m256i*)&mat2[k][j + 8]);
                    sumA_1 = _mm256_add_epi32(sumA_1, _mm256_mullo_epi32(bc_mat1_1, vecA_mat2));
                    sumB_1 = _mm256_add_epi32(sumB_1, _mm256_mullo_epi32(bc_mat1_1, vecB_mat2));
                    auto bc_mat1_2 = _mm256_set1_epi32(mat1[i + 1][k]);
                    sumA_2 = _mm256_add_epi32(sumA_2, _mm256_mullo_epi32(bc_mat1_2, vecA_mat2));
                    sumB_2 = _mm256_add_epi32(sumB_2, _mm256_mullo_epi32(bc_mat1_2, vecB_mat2));
                }
                _mm256_storeu_si256((__m256i*)&result[i][j], sumA_1);
                _mm256_storeu_si256((__m256i*)&result[i][j + 8], sumB_1);
                _mm256_storeu_si256((__m256i*)&result[i + 1][j], sumA_2);
                _mm256_storeu_si256((__m256i*)&result[i + 1][j + 8], sumB_2);
            }
        }
    }
}

Yet an other version, with even more tiling and with rearranging matrix 2. Added to the table of times above. Of course, some time can be saved if that matrix can be assumed to already be in that order, but I counted the rearranging in the benchmarks. That overhead scales as O(N²) while the meat of the algorithm scales as O(N³) so for a large matrix it does not represent a significant cost anyway.

It seems to behave well now, staying around 110% of the theoretical optimum for any size I test. Perhaps some small tweaks are still possible. For example, unrolling the i loop by 4 instead of 2 improved it slightly in my tests, but the difference is kind of small.

v5:

size_t ib = std::min(256, (int)N);
size_t jb = std::min(512, (int)N);
size_t kb = std::min(16, (int)N);

int *mat2 = (int*)_aligned_malloc(N * N * sizeof(int), 32);
size_t m2idx = 0;
for (size_t jj = 0; jj < N; jj += jb)
{
    for (size_t kk = 0; kk < N; kk += kb)
    {
        for (size_t j = jj; j < jj + jb; j += 16)
        {
            for (size_t k = kk; k < kk + kb; k++)
            {
                auto vecA_mat2 = _mm256_loadu_si256((__m256i*)&mat2in[k][j]);
                auto vecB_mat2 = _mm256_loadu_si256((__m256i*)&mat2in[k][j + 8]);
                _mm256_storeu_si256((__m256i*)&mat2[m2idx], vecA_mat2);
                _mm256_storeu_si256((__m256i*)&mat2[m2idx + 8], vecB_mat2);
                m2idx += 16;
            }
        }
    }
}

for (size_t ii = 0; ii < N; ii += ib) {
    for (size_t jj = 0; jj < N; jj += jb) {
        for (size_t kk = 0; kk < N; kk += kb) {
            for (size_t i = ii; i < ii + ib; i += 2) {
                for (size_t j = jj; j < jj + jb; j += 16) {
                    size_t m2idx = (j - jj) * kb + kk * jb + jj * N;
                    __m256i sumA_1, sumB_1, sumA_2, sumB_2;
                    if (kk == 0) {
                        sumA_1 = sumB_1 = sumA_2 = sumB_2 = _mm256_setzero_si256();
                    }
                    else {
                        sumA_1 = _mm256_load_si256((__m256i*)&result[i][j]);
                        sumB_1 = _mm256_load_si256((__m256i*)&result[i][j + 8]);
                        sumA_2 = _mm256_load_si256((__m256i*)&result[i + 1][j]);
                        sumB_2 = _mm256_load_si256((__m256i*)&result[i + 1][j + 8]);
                    }

                    for (size_t k = kk; k < kk + kb; k++) {
                        auto bc_mat1_1 = _mm256_set1_epi32(mat1[i][k]);
                        auto vecA_mat2 = _mm256_loadu_si256((__m256i*)(mat2 + m2idx));
                        auto vecB_mat2 = _mm256_loadu_si256((__m256i*)(mat2 + m2idx + 8));
                        sumA_1 = _mm256_add_epi32(sumA_1, _mm256_mullo_epi32(bc_mat1_1, vecA_mat2));
                        sumB_1 = _mm256_add_epi32(sumB_1, _mm256_mullo_epi32(bc_mat1_1, vecB_mat2));
                        auto bc_mat1_2 = _mm256_set1_epi32(mat1[i + 1][k]);
                        sumA_2 = _mm256_add_epi32(sumA_2, _mm256_mullo_epi32(bc_mat1_2, vecA_mat2));
                        sumB_2 = _mm256_add_epi32(sumB_2, _mm256_mullo_epi32(bc_mat1_2, vecB_mat2));
                        m2idx += 16;
                    }
                    _mm256_storeu_si256((__m256i*)&result[i][j], sumA_1);
                    _mm256_storeu_si256((__m256i*)&result[i][j + 8], sumB_1);
                    _mm256_storeu_si256((__m256i*)&result[i + 1][j], sumA_2);
                    _mm256_storeu_si256((__m256i*)&result[i + 1][j + 8], sumB_2);
                }
            }
        }
    }
}

_aligned_free(mat2);

[1] MSVC is a C++ compiler so my examples are technically not C, but it's really about the approach anyway and it's trivial to convert.


Simple MySql or MySqli Class Which one of these methods is better/safer to use? And what benefits could I get using one or other?

Simple mysqli:

connection.php

$DBServer = "localhost";
$DBPort = "3306";
$DBUser = "root";
$DBPass = "";
$DBName = "test";

$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName, $DBPort);
if ($conn->connect_error) {
    echo "Database connection failed: " . $conn->connect_error, E_USER_ERROR;
}
mysqli_set_charset($conn,"utf8");

index.php

include_once("connection.php");

$l_name = mysqli_real_escape_string($conn, $_GET['l_name']);

$query = mysqli_query($conn, "SELECT * FROM test WHERE lname='".$l_name."'");
while($row = mysqli_fetch_array($query)){
    echo $row['f_name'].' '.$row['l_name'].'
';
}

mysqli with class:

connection.php

class Connect
{

    var $host = 'localhost';
    var $user = 'root';
    var $pass = '';
    var $db = 'test';
    var $con;

    function connect() {
        $con = mysqli_connect($this->host, $this->user, $this->pass, $this->db);

        if (!$con) {
            //die('Could not connect to database!');
        } else {
            $this->con = $con; //echo 'Connection established!';
        }
        mysqli_set_charset($this->con,"utf8");

        return $this->con;

    }

    function close() {
        mysqli_close($con);
    }

}

index.php

include_once("connection.php");

$con = new Connect();
$con->connect();

$l_name = mysqli_real_escape_string($con->con, $_GET['l_name']);

$query = mysqli_query($con->con, "SELECT * FROM test WHERE lname='".$l_name."'");
while($row = mysqli_fetch_array($query)){
    echo $row['f_name'].' '.$row['l_name'].'
';
}

Can you review my code for security and best coding practices?

Don't use either - PDO is much more user-friendly and feature-rich.  Generally a class will be nicer to write code around, and now that we have PDO we don't need to write that class.  The following example is largely stolen from here: https://phpdelusions.net/pdo

$DBUser = "root";
$DBPass = "";
$DBName = "test";
$DBCharset = 'utf8mb4';

$dsn = "mysql:host=$DBServer;dbname=$DBName;charset=$DBCharset";
$DBOptions = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $DBUser, $DBPass, $DBOptions);
} catch (\PDOException $e) {
     // to avoid a potential credentials leak through a stack trace
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

$stmt = $pdo->prepare("SELECT * FROM test WHERE lname = ?");
$stmt->execute([$_GET['l_name']]);
$data = $stmt->fetchAll();

foreach ($data as $row) {
    echo "{$row['f_name']} {$row['l_name']}<br>";
}


How to get two JavaScript classes to talk to each other? I'm a budding JavaScript programmer, working on an exercise where I'm trying to create two classes (Books and Shelf) and need to have a shelf know what books are on it. I'm pretty new to OOP so I know I'm missing a crucial step (or three). I probably already ran off the rails, but any point in the right direction would be greatly appreciated.

<!DOCTYPE HTML>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Library</title>

There are multiple ways of handling this. If you can pass the Books you want to the Shelf object, that would be probably one of the easiest methods.

//The Shelves
function Shelf (shelfNumber, shelfName) {
    this.shelfNumber = shelfNumber;
    this.shelfName = shelfName;
    this.books = new Array();
    this.addBook = function(book) {
        this.books[this.books.length] = book;
    }
    this.displayBooks = function() {
        for(i=0; i < this.books.length; i++) {
            displayBooks(this.books[i]);
        }
    }
}

// then you can use it like this...

shelf100.addBook(bookA);
shelf100.addBook(bookB);
shelf100.addBook(bookC);

shelf100.displayBooks();


Avoiding a large 'switch' call in the main loop I am writing a Unix shell in C, which uses a rather large switch in the main loop.

It is used to handle built-in commands like 'cd' or'help'. The way it works is that it parses the user input and return a 'comand_t' struct, with 'name' being the first word of the input.

The in order to switch on a string, I translate it into an int by reading this initialized struct:

built_in_commands my_commands[CMD_NBR] = {
    {""     , EMPTY_CMD         , ""},
    {"cd"   , CD_CMD            , "change directory"},
    {"env"  , ENV_CMD           , "print enviroment. Can show single variable"},
    {"exit" , EXIT_CMD          , "exit from AGROS"},
    {"help" , HELP_CMD          , "print help"},
    {"?"    , SHORTHELP_CMD     , "print short help"}
};

The macros are defined in the header file:

#define EMPTY_CMD           0

#define CD_CMD              1
#define ENV_CMD             2
#define EXIT_CMD            3
#define HELP_CMD            4
#define SHORTHELP_CMD       5
#define SETENV_CMD          6
#define GETENV_CMD          7

#define OTHER_CMD           8

#define CMD_NBR             9

the main loop of the program is this:

while (AG_TRUE){

    /* Set the prompt */
    get_prompt(prompt, MAX_LINE_LEN, username);

    /*
     * Read a line of input
     * commandline should be deallocated with free()
     */
    commandline = read_input (prompt);

    parse_command (commandline, &cmd);

    switch (get_cmd_code (cmd.name)){ //returns an int
        case EMPTY_CMD:
            break;

        case CD_CMD:
            change_directory (cmd.argv[1], ag_config.loglevel);
            break;

        case HELP_CMD:
            print_help(&ag_config, cmd.argv[1]);
            break;

        case SHORTHELP_CMD:
            print_help(&ag_config, "-s");
            break;

        case ENV_CMD:
            print_env (cmd.argv[1]);
            break;

        case EXIT_CMD:
            free (commandline);
            commandline = (char *)NULL;
            closelog ();
            return 0;

        case OTHER_CMD:
            fork_and_exec();
            break;
    }
}

Every time I add a new builtin function, I wonder if there isn't a better way to do this.
What would it be? How can I call a function dynamically?

Function pointers

You want to use function pointers: http://www.newty.de/fpt/fpt.html

typedef void (*Command)(char const* args);  // Modify as required
                                            // But something like this
struct built_in_commands
{
    char const*   command;
    // int        commandID;   // Remove this you don't need it.
    char const*   man;
    Command       function;    // Add this: A pointer to a function
};

Then your switch becomes:

parse_command (commandline, &cmd);
if (cmd != NULL)
{    (*cmd->function)(commandline);      // Call the function associated with
}                                        // command (assuming you find it in the list)

Other Notes:

Basically, all of your function would have the same signature, and then be referred to inside of your struct instead of the number. Then you could call the function pointer from the struct and away you'd go.

        case EXIT_CMD:
            free (commandline);
            commandline = (char *)NULL;
            closelog ();
            return 0;

This appears to be the only place you free commandline. But you allocate it everytime you get a command. This means that you have a leak. There actually isn't much of a point in freeing the memory here because it will all be free as soon as the process terminates anyways.


Nested array-to-dict transformation I'm looking to let people define arbitrarily nested lists of lists and return a random json document for them. Right now the assumption is that if you have a structure like this:

[item1, item2, [item3, item4]]

The desired document is:

{item1: value,
item2: {
    item3: value,
    item4: value
    }
}

This is my current solution:

def random_document(document_layout, type_, **kwargs):
    document_layout.append("dummy_item")
    doc = lambda l:{l[i]:doc(l[i+1]) if isinstance(l[i+1],list) else random_item(type_,**kwargs) for i in range(len(l)-1)}
    return doc(document_layout)

as a one line fetishist, I feel like the middle line is beautiful, but I think the general hack of adding a dummy element to the end is hackish. Any thoughts on how to make this more elegant?

Also, not looking for deeply readable, this is just for my own private use and isn't going to spread out in any meaningful way, so I would just like to preempt the commentary about it not being readable.

Here's a more "readable" version:

def random_document(document_layout, type_, **kwargs):
    document_layout.append("dummy_item")
    full_dict = {}
    def internal_comprehension(document):
        for i in range(len(document)-1):
            if isinstance(document[i+1], list):
                full_dict.update({document[i]: internal_comprehension(document[i+1])})
            else:
                full_dict.update({document[i]: random_item(type_, **kwargs)})
    return internal_comprehension(document_layout)

Your one-liner is too unreadable for my taste.  Furthermore, neither version actually works.

The fundamental obstacle to elegance is that the problem requires you to look ahead one element to interpret how to handle the current element.

def random_document(document_layout, type_, **kwargs):
    def internal_comprehension(document):
        full_dict = {}
        for i in range(len(document)):
            if isinstance(document[i], list):
                # This item should have already been processed in a previous
                # recursive call to internal_comprehension()
                pass
            elif i == len(document) - 1 or not isinstance(document[i+1], list):
                # This item is an independent item
                full_dict.update({document[i]: random_item(type_, **kwargs)})
            else:
                # Next item is a list, so create a level of nesting
                full_dict.update({document[i]: internal_comprehension(document[i+1])})
        return full_dict
    return internal_comprehension(document_layout)

On the other hand, if you iterate through the document_layout backwards, then you don't have a look-ahead problem.  Here's how it could look like:

def random_document(document_layout, type_, **kwargs):
    dict = {}
    iter = reversed(document_layout)
    for k in iter:
        if isinstance(k, list):
            dict[iter.next()] = random_document(k, type_, **kwargs)
        else:
            dict[k] = random_item(type_, **kwargs)
    return dict


Using Wreq and Lens libraries to query Prosper for account info I am trying to create a program to manage my Prosper investments for me as an intro to Haskell project.  I created a post a few weeks ago for some of the simpler backend calculations: Recommend Next Note Purchase

Now I've started working on the web APIs in order to retrieve my account information from Prosper. Though it may not look it, the code below has already undergone some major refactoring to attempt to bring it to slightly readable.

This is my first attempt at any RESTful service interaction, JSON parsing, and usage of Lens.  I have a few areas of this code I would really appreciate some feedback.

I feel like I really went overboard with the let bindings, but I couldn't figure out how to break up some absurdly long lines in another way.
All my maybeXXX functions.  I needed them to operate on the Maybe typeclass in the way I was expecting, but I feel like there is a more elegant use of Monads that could avoid these auxiliary functions.
The creation of the MResult type was done just to parse the response body of the web API.  There must be a way to use Lens to extract that field and parse it as a list of Note directly.

Any other feedback on the general structure or style of the code is greatly appreciated.

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}

import Prosper
import Network.Wreq
import Control.Lens
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy.Internal as BL
import qualified Data.Text as T
import Data.Text.Encoding
import Data.Aeson
import Data.Aeson.Lens
import GHC.Generics

data Note = Note {
    loan_number :: Int,
    prosper_rating :: T.Text
    } deriving (Generic, Show)
instance FromJSON Note

data MResult = MResult {
    result :: [Note]
    } deriving (Generic, Show)
instance FromJSON MResult

-- String Constants
prosperAddress = "https://api.prosper.com/v1/"
authTarget = "security/oauth/token"
accountTarget = "accounts/prosper/"
notesTarget = "notes/"
clientID = "cicicicici"
clientSecret = "ssssssssssssssss"
userID = "uuuuuuuuuu"
password = "pppppppppp"

body :: BS.ByteString
body =  strToBS $
            "grant_type=password" ++
            "&client_id=" ++ clientID ++
            "&client_secret=" ++ clientSecret ++
            "&username=" ++ userID ++
            "&password=" ++ password
    where strToBS = encodeUtf8 . T.pack

-- Retrives the OAuth2 token from the Prosper server
oauthToken :: IO T.Text
oauthToken = do
    let opts = defaults & header "Accept" .~ ["application/json"]
                        & header "Content-type" .~ ["application/x-www-    form-urlencoded"]
    resp <- postWith opts (prosperAddress ++ authTarget) body
    return $ resp ^. responseBody . key "access_token" . _String

-- Performs a GET request to the provided target using the provided OAuth     token
getTarget target token = do
    let tokenStr = encodeUtf8 $ T.append "bearer " token
    let opts = defaults & header "Authorization" .~ [tokenStr]
    getWith opts (prosperAddress ++ target)

-- Retrives the list of currently owned notes from Prosper
-- Note: The query responses are paginated to 25 results per response
getNotesList :: T.Text -> Int -> IO [T.Text]
getNotesList token offset = do
    let target = notesTarget ++ "?offset=" ++ (show offset)
    resp <- getTarget target token
    let r =  fmap result (decode $ resp ^. responseBody :: Maybe MResult)
    let notes = fmap (map prosper_rating) r
    if (maybeLength notes) /= 25 then
        maybeReturn notes
        else do
            restOfNotes <- getNotesList token (offset+25)
            return $ maybeAppend notes restOfNotes

maybeLength Nothing = 0
maybeLength (Just xs) = length xs

maybeReturn Nothing = return []
maybeReturn (Just xs) = return xs

maybeAppend :: Maybe [a] -> [a] -> [a]
maybeAppend Nothing xs = xs
maybeAppend (Just ys) xs = ys ++ xs

main = do
    token <- oauthToken
    notes <- getNotesList token 0
    print $ recommendNote notes [0,0,0.2,0.2,0.25,0.30,0.05]

You're on the right path! You're asking great questions.

  I feel like I really went overboard with the let bindings, but I couldn't figure out how to break up some absurdly long lines in another way.

This is sort of unavoidable with wreq (or any HTTP client library in general probably), as you have to pass around a lot of state to communicate on a stateless protocol. You do a little better if you pass your options as a single Options value. Leads to friendlier type signatures too.

  All my maybeXXX functions. I needed them to operate on the Maybe typeclass in the way I was expecting, but I feel like there is a more elegant use of Monads that could avoid these auxiliary functions.

You can use fmap or <$> instead of constructing these maybeWobble helpers, but as we see below there's an even better option.

  The creation of the MResult type was done just to parse the response body of the web API. There must be a way to use Lens to extract that field and parse it as a list of Note directly.

There is! Your usage of the lens-aeson package is right on the money. To grab all the prosper_rating :: Text of all the notes in the result key, we can simply write this lens traversal:

_ratings = responseBody . key "result"
                        . _Array
                        . traverse
                        . key "prosper_rating"
                        . _String

(Traversal just means that you need to use ^.. instead of ^. and that you'll get back a list of values instead of one when you do so.)

The other way you could be using more lens is to import the text lens module, which provides two "once you learn of them you can't stop using them" helpers:

packed, unpacked: isomorphisms between Text and String. Never import qualified Data.Text again!
utf8: a prism that converts a ByteString to a Text, when possible. Never import qualified Data.Bytestring again!
_Show: a prism (in Control.Lens actually, but we'll toss it onto this list) that lets you convert any Show a => a value into String. Earn a combo bonus when you compose it with packed to get a Text.

One other note is that you should use param and := from the wreq library to construct parameter strings and POST bodies. They handle the text massaging for you.

OK, putting everything together:

{-# LANGUAGE OverloadedStrings #-}

module Free_D where

import Control.Lens
import Data.Aeson
import Data.Aeson.Lens
import Data.Monoid ((<>))
import Data.Text.Strict.Lens
import Network.Wreq

import Data.Text (Text)

-- String Constants
prosperAddress, clientID, clientSecret, userID, password :: Text
prosperAddress = "https://api.prosper.com/v1/"
clientID = "cicicicici"
clientSecret = "ssssssssssssssss"
userID = "uuuuuuuuuu"
password = "pppppppppp"

prosperURL :: Text -> String
prosperURL target = (prosperAddress <> target) ^. unpacked

-- | Retrives the OAuth2 token from the Prosper server.
oauthToken :: IO Text
oauthToken = do
    let opts =
          defaults
            & header "Accept" .~ ["application/json"]
            & header "Content-Type" .~ ["application/x-www-form-urlencoded"]
    let body =
          [ "grant_type" := ("password" :: Text)
          , "client_id" := clientID
          , "client_secret" := clientSecret
          , "username" := userID
          , "password" := password]
    resp <- postWith opts (prosperURL "security/oauth/token") body
    return (resp ^. responseBody . key "access_token" . _String)

-- | Performs a GET request to the provided target using the provided
-- OAuth token.
getTarget :: Options -> String -> Text -> IO (Response Value)
getTarget opts url token = do
    getWith (opts & header "Authorization" .~ rawr) url >>= asJSON
    where
        rawr = ["bearer " <> utf8 # token]

-- | Retrives the list of currently owned notes from Prosper.
--
-- Note: The query responses are paginated to 25 results per response.
getNotesList :: Text -> Int -> IO [Text]
getNotesList token offset = do
    resp <- getTarget opts (prosperURL "notes/") token
    let ratings = resp ^.. _ratings
    remainder <- if length ratings == 25 then return [] else getNotesList token (offset + 25)
    return (ratings ++ remainder)
    where
        opts = defaults & param "offset" .~ [offset ^. re _Show . packed]
        _ratings = responseBody . key "result" . _Array . traverse . key "prosper_rating" . _String

main :: IO ()
main = do
    token <- oauthToken
    notes <- getNotesList token 0
    print notes

(I got the code compiling but, as I don't have an account, I was unable to run and test it.)


How to use the value of a method result based on a test? I have some Java like this:

if (!fooclass.getInfoText().equals(""))
{
    this.setInfoText(fooclass.getInfoText());
}

Is there a neater way of setting one value based on the test results of another value? It seems nasty to me that the method is run twice, but putting the result in a variable when it may be empty and not used at all seems equally messy.

In PHP I might do:

if (($foo = $bar->baz()) !== "")
{
    $this->set_info_text = $foo;
}

But I know some people are quite strongly for and against this method, though I can't recall a good justification against it.

I believe fooclass is a reference of Foo class. I can only think about introducing a variable and check it to prevent the double-invokation of same method.

String fooInfo = fooclass.getInfoText();
if (!"".equals(fooInfo))
{
    this.setInfoText(fooInfo);
}

NOTE

I'm using "".equals(fooInfo) to avoid NPE.


PHP surround search text with words The requirement: search a string in a text-string (from DB) and show additional info around when presenting the result.

Examples:

text string = "The Quick Brown Fox Jumps Over the Lazy Dog"

search ==> result:

"The" ==> "The Quick ..."
"Quick" ==> "The Quick Brown ..."
"Brown" ==> "... Quick Brown Fox ..."
"Lazy" ==> "... the Lazy Dog"
"ump" ==> "... Fox Jumps Over ..."

Rules are:

aways complete word containing search string
the word before if exists
the word after if exists
if there is something before the "before" word, show "..." at beginning
if there is something after the "after" word, show "..." at end

How I solved it:

// initialize text
$text_found = '';
// first eliminate line feeds, tabs, double spaces etc
$tmp = str_replace(array( "\r\n", "\r", "\n", "\t", "  " ), ' ', $text );
// now get starting position
$posStart = strpos( mb_convert_case( $tmp, MB_CASE_LOWER, 'UTF-8' ), $search );
// search first space before position
$space = strrpos( substr( $tmp, 0, $posStart +1 ), ' ', -1 );
if ( $space === FALSE ):
    // no earlier space found, so start from beginning
    $posStart = 0;
else:
    $posStart = $space;
    // check if there is an additional word before
    $space = strrpos( substr( $tmp, 0, $space ), ' ', -1 );
    if ( $space === FALSE ):
        $text_found .= substr( $tmp, 0, $posStart );
    else:
        $text_found .= '... ' . substr( $tmp, $space, $posStart - $space );
    endif;
endif;
// now we search first space after position
$space = strpos( substr( $tmp, $posStart +1 ), ' ' ) + 1;
if ( $space === FALSE ):
    // no space after found, so add text till end
    $text_found .= substr( $tmp, $posStart );
else:
    // check if there is an additional word after
    $space2 = strpos( substr( $tmp, $posStart + $space + 1 ), ' ' );
    if ( $space2 === FALSE ):
        $text_found .= substr( $tmp, $posStart );
    else:
        $text_found .= substr( $tmp, $posStart, $space2 + $space + 1 ) . ' ...';
    endif;
endif;

Question is if there is a more elegance solution ?
And if so how ?

Please see post below with my final outcome.

You say you want to search a text string from a database, but are not showing the database part.  This is key to think about, as I am guessing you could cut out all this work if you made appropriate search requests against an appropriately designed data store.  Most any modern relational database supports some level of natural language search, which would allow you to actually do this across more than a single string at a time.

This is not to mention the existence of data stores both open source and commercial geared specifically towards search cases, even allowing the ability to return search matches with context as you are trying to do.

Consider doing this sort of work with a tool that is optimized for this purpose.

Thoughts on code:

You are clearly working with multibyte strings yet are not using the multibyte versions of the the standards string manipulation functions.  This could lead to unexpected behavior. If you need to have string functions overloaded with mb counterparts, you might consider a comment in the code noting that this PHP setting needs to be turned on for this code to operate properly.
you should add vertical white space to you code around logical sections to help ease of reading. Think of empty lines like you would paragraphs in writing. They help break up your thoughts.
you are casting to lowercase only on haystack and not on needle.  Why is this? Typically if you are doing case insensitive search, you would cast both in same manner.
you may consider using regex with appropriate substring pattern capture and compare from a performance standpoint. I am not sure if it will perform as well, but it could certainly simplify your code, which can sometimes holds more value if performance is not too big of a trade off.
your code isn't designed to handle multiple matches. This may be OK with regards to the design, but figured I would point this out.  You could take your example haystack and search for "the", and never get the second occurrence found.
what happens if space is passed as search string?
what happens if single character is passed or other similar trivial or ambiguous search term? Do you have something up the call stack which weeds out these potentially undesirable use cases?


Filter for reading data over a serial connection This is a simple filter that I have been using for a project reading data over a serial connection, and thought it would be good to use it as my first attempt to write docstrings. Does anyone have any suggestions? I have been reading PEP 257. As it is a class, should the keyword arguments come after the __init__ ?

If there is a better way to write any part of it (not only the docstrings), I would appreciate it if people could point me in the right direction.

class Filter(object) :
    """Return x if x is greater than min and less than max - else return None.

    Keyword arguments:
    min -- the minimum (default 0)
    max -- the maximum (default 0)

    Notes:
    Accepts integers and integer strings
    """
    def __init__(self, min=0, max=0) :
        self.min = min
        self.max = max
    def __call__(self, input) :
        try :
            if int(input) <= self.max and int(input) >= self.min :
                return int(input)
        except : pass

class Filter(object) :
    """Return x if x is greater than min and less than max - else return None.

How about "if x is between min and mix"?

    Keyword arguments:
    min -- the minimum (default 0)
    max -- the maximum (default 0)

    Notes:
    Accepts integers and integer strings
    """
    def __init__(self, min=0, max=0) :

min and max are builtin python functions. I suggest not using the same names if you can help it.

        self.min = min
        self.max = max
    def __call__(self, input) :
        try :
            if int(input) <= self.max and int(input) >= self.min :

In python you can do: if self.min < int(input) <= self.max

                return int(input)

        except : pass

Don't ever do this. DON'T EVER DO THIS! ARE YOU LISTENING TO ME? DON'T EVER DO THIS.

You catch and ignore any possible exception. But in python everything raises exceptions, and you'll not be notified if something goes wrong. Instead, catch only the specific exceptions you are interested in, in this, case ValueError.

Futhermore, having a filter that tries to accept string or integers like this just a bad idea. Convert your input to integers before you use this function, not afterwards.

The interface you provide doesn't seem all that helpful. The calling code is going to have to deal with None which will be a pain. Usually in python we'd do something operating on a list or as a generator.

Perhaps you want something like this:

return [int(text) for text in input if min < int(text) < max]


Reading binary file of doubles I'm trying to read doubles from a relatively small binary file. This currently reads a 100 KB file in about 6 milliseconds in my system. I would like to reduce that if possible.

void readNParseData(const char* filePath, vector<double> *&data){

    ifstream ifs(filePath, ios::in | ios::binary);

    // If this is a valid file
    if (ifs) {
        // Temporary Variables
        std::streampos fileSize;
        double *fileBuffer;
        size_t sizeOfBuffer;

        // Check whether the parameter is already full
        if (data != 0){
            // Reset the output
            data->clear();
            data = 0;
        }

        // Get the size of the file
        ifs.seekg(0, std::ios::end);
        fileSize = ifs.tellg();
        ifs.seekg(0, std::ios::beg);

        sizeOfBuffer = fileSize / sizeof(double);
        fileBuffer = new double[sizeOfBuffer];

        ifs.read(reinterpret_cast<char*>(fileBuffer), fileSize);

        // Now convert the double array into vector
        data = new vector<double>(fileBuffer, fileBuffer + sizeOfBuffer);

        free(fileBuffer);
    }
}

As you can see there is a redundant copy of a double * array to a vector. Perhaps reading to the vector directly might speed it up, but I don't know how.

Suggestion 1

This block of code does not seem clean:

    // Check whether the parameter is already full
    if (data != 0){
        // Reset the output
        data->clear();
        data = 0;
    }

If data used to point to non-NULL, then you are just making it NULL. You should delete data before pointing it to NULL:

    if (data != 0){
        // Reset the output
        data->clear();
        delete data;
        data = 0;
    }

Suggestion 2

Still better, if you have the option, change the interface to

void readNParseData(const char* filePath, vector<double>& data);

Suggestion 3

There is nothing in your code to indicate to the calling function that you were able or unable to read the data from the file. There is no else to go with

if (ifs) {

One way of indicating whether the function was successful in reading the data is to change the return type of the function to bool. Then, you can add

return true;

at the end of the if block and then add an else block:

else {
   return false;
}

Suggestion 4

To remove the redundant memory allocation and deallocation to fileBuffer, simply use std::vector::data:

data.resize(sizeOfBuffer);
ifs.read(reinterpret_cast<char*>(data.data()), fileSize);

Suggestion 5

Add a check to make sure that ifs.read was successful:

ifs.read(reinterpret_cast<char*>(data.data()), fileSize);
if ( !ifs )
{
   return false;
}


Hash function in C for sets and associative arrays I wrote a non-cryptographic hash function in C, which I am hoping might be suitable for use in an implementation of hash sets or hash maps.

uint_fast32_t hash_u32(uint_fast32_t x) {
    uint_fast32_t h = x;
    h = h ^ (h>>16);
    h = h * 0xae6a495b;
    h = h ^ (h<<16);
    h = h * 0xae6a495b;
    return h;
}

I've tested this integrated into a set implementation before, which can be seen here, and it seems to be working fine, but I'm interested in knowing whether this is a good approach to integer hashing, or general hashing assuming all problem domains can be reduced to map the value to an integer and then hash that.

... in knowing whether this is a good approach ...

Small improvements

Keep in range

uint_fast32_t may be wider than 32 bits.  Values outside the 32-bit range in x will have a result that depends on those excessive upper bits.  I'd suggest coding to insure 1) only the lower 32 are used to form the answer and 2) the answer only has lower 32-bits set.  #2 being an important aspect here - don't generate results outside 32-bits.  When uint_fast32_t is 32-bit, a good compiler will optimize away the & 0xFFFFFFFF masks.

uint_fast32_t h = x & 0xFFFFFFFF;
...
return h & 0xFFFFFFFF;

Readability

Style issue:

I find reading hexadecimal constants easiest to read when the case of the x differs from the digits.

// h = h * 0xae6a495b;
h = h *    0xAE6A495B;

Avoid naked magic numbers.

I did not note the 2 hex constants were the same until a bit later.  Defines/const objects would have helped and self-document the code.

const uint_fast32_t hash_m = 0xAE6A495B;
const uint_fast32_t uint32_mask = 0xFFFFFFFF;
const unsigned hash_shift = 16;

uint_fast32_t h = x & uint32_mask;
h = h ^ (h >> hash_shift);
h = h * hash_m;
h = h ^ (h << hash_shift);
h = h * hash_m;
return h & uint32_mask;

Why 32 for a general approach?

With "good approach to integer hashing or general hashing" seems to assume an "integer" is 32 bits.  Such an assumptions breaks the "general hashing".  For a "general hashing" I would use unsigned or perhaps size_t and steer the constants based on the type's range.  Either that or forgo the "general" adjective to this code and simply say its is designed for 32-bit as hash_u32() implies.

uint_fast32_t vs. uint32_t

uint_fast32_t has 2 advantages, 1) it always exists since C99 as uint32_t may not exist on rare machines that lack 32-bit types.  2) It may be "faster".

Yet uint_fast32_t is also problematic.  It is more difficult to test.  Proper function testing obliges a test on machines where uint_fast32_t is 32 and more than 32-bit as the above masking discussion points out potential pitfalls on the rarer  wide uint_fast32_t.

I doubt code was written to take advantage of #1 (uint32_t may not exist).

Unless #1 is a design concern, best, especially for a "general" function, to stick to fixed width or regular types and reserve the "fast" types for internal function usage and not as a function parameter nor return value.  Using "fast" as part of the function I/F, may be useful in select cases, but not "general".

Missing header

I'd expect #include <stdint.h> as part of the code to define uint_fast32_t, else code does not compile.

Good Hash?

I threw together a quick test to see how  32-bit values mapped to what.

Note: On my machine with a 64-bit uint_fast32_t, code died on flag[i] unless OP's function had h masked with h & 0xFFFFFFFF.

#include <stdint.h>
uint_fast32_t hash_u32(uint_fast32_t x) {
  uint_fast32_t h = x & 0xFFFFFFFF;
  h = h ^ (h >> 16);
  h = h * 0xae6a495b;
  h = h ^ (h << 16);
  h = h * 0xae6a495b;
  return h & 0xFFFFFFFF;
}

#include <assert.h>
#include <stdio.h>
void hash_u32_test(void) {
  _Static_assert(CHAR_BIT == 8, "CHAR_BIT != 8");
  unsigned char *flag = calloc(1, 1u << (32 - 3));
  if (!flag) return;
  uint32_t twice = 0;
  uint32_t x = 0;
  do {
    uint_fast32_t y = hash_u32(x);
    uint_fast32_t i = y / 8;
    uint_fast32_t j = y % 8;
    if (flag[i] & (1u << j)) {
      twice++;
    }
    flag[i] |= (1u << j);
    x++;
  } while (x);
  printf("twice %lu\n", (unsigned long) twice);
}

int main() {
  hash_u32_test();
  return 0;
}

My result was twice 0 implying a one-one mapping of all 32-bit inputs to output, so from a hash perspective, that is good for OP's function to pass.


Help me optimize this scrollTo I'm no pro in JavaScript. Please help me review this code. I would appreciate any suggestions!

JavaScript:

var y_offset, /*current position of window*/
    distance_from_current_position, /*different between current position and target*/
    body_height, /*hight of body*/
    window_height, /*height of window*/
    position_of_target, /*target position*/
    max_scroll, /*max position to scroll to*/
    up_or_down, /*scroll up or down*/
    my_classes = document.querySelectorAll('.y');

function scroll_function(target) {

    y_offset = window.pageYOffset;

    distance_from_current_position = target - window.pageYOffset;

    setTimeout(function () {

        if (distance_from_current_position !== 0) {
            if (up_or_down) {
                if (y_offset < target - 5) {
                    window.scroll(0, y_offset + (distance_from_current_position / 3))
                } else {
                    window.scroll(0, y_offset + 1)
                }
                if (y_offset < position_of_target - 1) {
                    scroll_function(target);
                }
            } else {
                if (y_offset > target + 5) {
                    window.scroll(0, y_offset + (distance_from_current_position / 3))
                } else {
                    window.scroll(0, y_offset - 1)
                }
                if (y_offset > position_of_target + 1) {
                    scroll_function(target);
                }
            }
        }

    }, 50)
}

function click_function(i) {
    my_classes[i].onclick = function () {
        body_height = document.body.clientHeight;

        position_of_target = document.getElementById(this.getAttribute('data-y')).offsetTop;

        window_height = window.innerHeight;

        max_scroll = body_height - window_height;

        if (window.pageYOffset < position_of_target) {
            up_or_down = 1
        } else {
            up_or_down = 0
        }

        if (position_of_target > max_scroll) {
            position_of_target = max_scroll;
        }
        $y_1(position_of_target);

    };

}

for (var i = 0; i < my_classes.length; i++) {
    click_function(i);
}

HTML:

<!DOCTYPE html>
<title></title>

Scroll to id1

Scroll to id2

Scroll to id´3

From a once over:

This code does not work, you did not provide $y_1, I am assuming you meant scroll_function
I built a JsBin for this : http://jsbin.com/sihoj/1/edit, because you keep calling scroll_function, you can actually prevent the user from reaching the navigation links again, that's not a good approach
As James Khoury mentioned, why not use <a href="#elementid"> or scrollTo ?
You should use lowerCamelCase
This :

if (position_of_target > max_scroll) {
    position_of_target = max_scroll;
}

could be

position_of_target = Math.min( position_of_target , max_scroll );

I feel this reflects better what you are trying to accomplish

Naming

I would much rather see

//Distance from currrent position
distance = target - window.pageYOffset;

than

distance_from_current_position = target - window.pageYOffset;

your code becomes a bit unwieldy when you have too many long variable names

If you have target and would have distance , then I would also go for offset, y_offset looks ugly.

Also here

position_of_target = document.getElementById(this.getAttribute('data-y')).offsetTop;

you should have some deep thoughts about the variable name, position implies usually an x/y coordinate, but you are really dealing here with an offset from the top.

Finally : up_or_down is a terrible name, there is no logical connection for 1 being up, and 0 being down. At the very least you should consider
var UP = true, DOWN = false or var UP = 1, DOWN = 0 if you don't want to use booleans.

Magic Constants

/ 3 <- That 3 should be a well named constant
target - 5 <- That 5 should be a well named constant

Fake Booleans

This:

    if (window.pageYOffset < position_of_target) {
        up_or_down = 1
    } else {
        up_or_down = 0
    }

is letting up_or_down be a fake boolean, you should go for

    up_or_down = (window.pageYOffset < position_of_target)

especially since you are doing falsey comparisons to up_or_down any way.

DRY

This:

        if (up_or_down) {
            if (y_offset < target - 5) {
                window.scroll(0, y_offset + (distance_from_current_position / 3))
            } else {
                window.scroll(0, y_offset + 1)
            }
            if (y_offset < position_of_target - 1) {
                scroll_function(target);
            }
        } else {
            if (y_offset > target + 5) {
                window.scroll(0, y_offset + (distance_from_current_position / 3))
            } else {
                window.scroll(0, y_offset - 1)
            }
            if (y_offset > position_of_target + 1) {
                scroll_function(target);
            }
        }

could be

    var vector = up_or_down ? -1 : +1;

    if (y_offset < target + 5 * vector ) {
        window.scroll(0, y_offset + (distance_from_current_position / 3))
    } else {
        window.scroll(0, y_offset - vector)
    }
    if (y_offset < position_of_target + vector) {
        scroll_function(target);
    }


Converting seconds to hours, minutes and seconds The following question was taken from Absolute Java 5th ed. by Walter Savitch:

  Write a program that outputs the number of hours, minutes, and seconds that  corresponds to 50,391 total seconds. The output should be 13 hours, 59 minutes, and 51 seconds. Test your program with a different number of total seconds to ensure that it works for other cases.

This is the code that I have written:

    public class Question7 {

    private static final int MINUTES_IN_AN_HOUR = 60;
    private static final int SECONDS_IN_A_MINUTE = 60;

    public static void main(String[] args) {

        int seconds = 50391;

        System.out.println(timeConversion(seconds));

    }

    private static String timeConversion(int totalSeconds) {
        int hours = totalSeconds / MINUTES_IN_AN_HOUR / SECONDS_IN_A_MINUTE;
        int minutes = (totalSeconds - (hoursToSeconds(hours)))
                / SECONDS_IN_A_MINUTE;
        int seconds = totalSeconds
                - ((hoursToSeconds(hours)) + (minutesToSeconds(minutes)));

        return hours + " hours " + minutes + " minutes " + seconds + " seconds";
    }

    private static int hoursToSeconds(int hours) {
        return hours * MINUTES_IN_AN_HOUR * SECONDS_IN_A_MINUTE;
    }

    private static int minutesToSeconds(int minutes) {
        return minutes * SECONDS_IN_A_MINUTE;
    }
}

Your usage of auxiliary functions is not consistent: Some computations are
done inline, e.g.

int hours = totalSeconds / MINUTES_IN_AN_HOUR / SECONDS_IN_A_MINUTE;

and other (equally simple) computations are done using the functions
hoursToSeconds() and minutesToSeconds().

But my main point is that the calculation is too complicated, and extending
it to more time units (e.g. days) would make it even more complicated.

It is easier if you compute the lowest time unit first (seconds)
using the remainder operator %,
and then proceed to the higher time units (minutes, then hours).

Then you don't need additional functions for the conversion and therefore you
can restrict the scope of the conversion constants to
the timeConversion() function.

private static String timeConversion(int totalSeconds) {

    final int MINUTES_IN_AN_HOUR = 60;
    final int SECONDS_IN_A_MINUTE = 60;

    int seconds = totalSeconds % SECONDS_IN_A_MINUTE;
    int totalMinutes = totalSeconds / SECONDS_IN_A_MINUTE;
    int minutes = totalMinutes % MINUTES_IN_AN_HOUR;
    int hours = totalMinutes / MINUTES_IN_AN_HOUR;

    return hours + " hours " + minutes + " minutes " + seconds + " seconds";
}


Handling more websocket connections I have the following basic code, which connects to a websocket server and receives some data:
import websocket, json, time

def process_message(ws, msg):
    message = json.loads(msg)
    print(message)

def on_error(ws, error):
    print('Error', e)

def on_close(ws):
    print('Closing')

def on_open(ws):
    def run(*args):
        Subs = []

        tradeStr=  """{"method": "SUBSCRIBE", "params":%s, "id": 1}"""%(json.dumps(Subs))
        ws.send(tradeStr)

    thread.start_new_thread(run, ())

def Connect():
    websocket.enableTrace(False)
    ws = websocket.WebSocketApp("wss://myurl", on_message = process_message, on_error = on_error, on_close = on_close)
    ws.on_open = on_open
    ws.run_forever()

Connect()

Now, I would like to create more connections to different servers and receive data concurrently in the same script. I tried the following:
def run(url):

    def process_message(ws, msg):
        message = json.loads(msg)
        print(message)

    def on_error(ws, error):
        print('Error', e)

    def on_close(ws):
        print('Closing')

    def on_open(ws):
        def run(*args):
            Subs = []

            tradeStr=  """{"method": "SUBSCRIBE", "params":%s, "id": 1}"""%(json.dumps(Subs))
            ws.send(tradeStr)

        thread.start_new_thread(run, ())

    def Connect():
        websocket.enableTrace(False)
        ws = websocket.WebSocketApp(url, on_message = process_message, on_error = on_error, on_close = on_close)
        ws.on_open = on_open
        ws.run_forever()

    Connect()

threading.Thread(target=run, kwargs={'url': 'url1'}).start()
threading.Thread(target=run, kwargs={'url': 'url2'}).start()
threading.Thread(target=run, kwargs={'url': 'url3'}).start()

Now, this code works, but I'm connecting to different URLs and I'm streaming data from all of them, but it seemed to me an "hacky" solution. I also don't know if what I'm doing could be bad practice or not. Each connection will send around 600/700 small JSON dictionaries, and I need to update every record to the db.
So my question is: is this implementation ok? Since it works with threads, can it create problems in the long run? Should I do another library such as Tornado?

Welcome to code review community. Few initial thoughts, when writing python code; following PEP-8 style guide makes your code more maintainable. Which would amount to (but not limited to):

Functions and variables named in lower_snake_case.
Avoiding whitespaces in function parameters/arguments.

Moving on, you can put your websocket handling code in its own submodule. For eg.
class WebSocket:
    def __init__(self, url):
        self._url = url

    def close(self, ws):
        # handle closed connection callback
        pass

    def connect(self):
        self.ws = websocket.WebSocketApp(self._url, on_close=self.close)
        self.ws.on_open = self.open  # for eg.
        self.ws.run_forever()
    .
    .
    .

If you want it to support threading, you can extend this class with threading.Thread sub-classing (you may also choose to subclass multiprocessing.Process later):
class ThreadedWebSocket(WebSocket, threading.Thread):
    def run(self):
        # This simply calls `self.connect()`

this way, if you want to initialise threads:
url1_thread = ThreadedWebSocket(url=url1)
url1_thread.start()
url1_thread.join()

The subclassing is mostly a matter of preference.

In the on_open callback, you are dumping a dict to json and then inserting it to a string template. The following achieves the same result, and is more intuitive (imo):
def on_open(ws):
    def run(*args):
        subs = []
        trade =  {
            "method": "SUBSCRIBE",
            "params": subs,
            "id": 1
        }
        ws.send(json.dumps(trade))

Coming to your question about library alternates, and performance, network I/O is generally a blocking call. This was one of the major reason I suggested going with the websockets package. The other being; we've been using websockets at our organisation for over an year in production. And it has been working really impressively for us, handling a throughput of few GB data everyday with a single low-tier machine.


Advent of Code 2019 day 2 in F# I sped through the 5hr F# pluralsight course this week-end and thought I'd sement my learning by going through as many Advent of Code puzzles I could with this new learning. However, already on day 2 I'm starting to question whether I'm too biased from C syntax and imperative coding to do it "right".

I really really miss being able to break from for loops for the second solution here. Sure I could make a while loop and have a mutable i and j, but it seems wrong either way. Also, I'd like function pointers for the "ops", but that's just a matter of doing some more work I guess.

In any case, some pointers on things to improve and make more declarative in the code would be super appreciated. Would really like to get going on the right foot before running off to day 3.

open System
open System.IO

let parse = fun (x:String) -> x.Split ',' |> Array.map int

let run (ops:int[]) =
    let mutable i = 0
    while i < ops.Length do
        match ops.[i] with
            | 1 ->
                let a = ops.[ops.[i+1]]
                let b = ops.[ops.[i+2]]
                ops.[ops.[i+3]] <- a + b
                i <- i + 4
            | 2 ->
                let a = ops.[ops.[i+1]]
                let b = ops.[ops.[i+2]]
                ops.[ops.[i+3]] <- a * b
                i <- i + 4
            | 99 ->
                i <- ops.Length
            | _ ->
                printfn "doh %A" i
    ops

let test = parse >> run

let test1 = test "1,0,0,0,99"
let test2 = test "2,3,0,3,99"
let test3 = test "2,4,4,5,99,0"
let test4 = test "1,1,1,4,99,5,6,0,99"

let input = parse <| File.ReadAllText (Path.Combine [|__SOURCE_DIRECTORY__;@"input.txt"|])
let prog1 = Array.copy input
prog1.[1..2] <- [|12;2|]
run prog1 |> ignore

let exp = 19690720
let mutable solNounVerb = (0, 0)
for i = 0 to 100 do
    for j = 0 to 100 do
        let prg = Array.copy input
        prg.[1..2] <- [|i;j|]
        run prg |> ignore
        if prg.[0] = exp then
            solNounVerb <- (i, j)

let sol2 = (fun (i, j) -> i * 100 + j) solNounVerb

Possibly not the best solution, but here's one that uses F# more extensively:

let rec processProgram (i: int) (ops: int list) =
    match ops with
    | opCode :: aPos :: bPos :: outPos :: rest  when opCode = 1 -> // add op code
        let a = ops |> List.item aPos
        let b = ops |> List.item bPos
        let complete = [opCode; aPos; bPos; outPos] @ processProgram (i + 4) rest
        let (left, right) = complete |> List.splitAt outPos
        left @ [a + b] @ (right |> List.skip 1)
    | opCode :: aPos :: bPos :: outPos :: rest when opCode = 2 -> // multiple op code
        let a = ops |> List.item aPos
        let b = ops |> List.item bPos
        let complete = [opCode; aPos; bPos; outPos] @ processProgram (i + 4) rest
        let (left, right) = complete |> List.splitAt outPos
        left @ [a * b] @ (right |> List.skip 1)
    | head :: rest  when head = 99 -> // terminate
        ops
    | _ ->
        failwith "Unsported opcode"

I've turned the function into a recursive function with let rec so that it can be called from within itself, I'll generally use this rather than a loop.

Next I'm using list rather than array so that I can pattern match on the operations. Let's look at that:

match ops with
| opCode :: aPos :: bPos :: outPos :: rest  when opCode = 1 -> // add op cod

This expects the list to have at least 4 elements, and I'm destructing it into the pieces we care about, opCode (what we're doing), aPos and bPos (where in the instruction set the values we want are) and outPos (where to put the output in the instruction list). It's then got a when test on the end to check the value of the opCode so we can hit the right op code branch.

When the match is hit we grab the values from their current pos and then process the rest of the instruction set, generating a new list with:

let complete = [opCode; aPos; bPos; outPos] @ processProgram (i + 4) rest

The @ operator concats two list's together.

Then we'll split it on the outPos:

let (left, right) = complete |> List.splitAt outPos

This gives us a tuple of int list * int list that we deconstruct into two variables and lastly we rebuild the list through concatinations:

left @ [a + b] @ (right |> List.skip 1)

We use the left part to start with, as it is all up until the outPos, then insert the result of the op-code (add in this place) and then concat-ing the right but skipping outPos which is the head of the list.

Side note: it's not stated in the requirements that you can update positions beyond the current op stack, so I didn't implement that, it'll only do reverse-updates.


Posts that probably need editing I wrote this Data Explorer query (reproduced below) some time ago that helped identify posts containing potential spelling/punctuation errors and earned me a Copy Editor badge on Anime.SE.

SELECT DISTINCT PostTypes.Name AS [Post Type],
                Posts.Score AS [Post Score],
                Posts.Id AS [Post Link],
                Posts.LastActivityDate AS [Last Activity]
FROM Posts
INNER JOIN PostTypes
ON Posts.PostTypeId = PostTypes.Id
INNER JOIN Users
ON Posts.OwnerUserId = Users.Id
INNER JOIN PostHistory
ON Posts.Id = PostHistory.PostId
WHERE Posts.Score > 0 AND -- Low quality posts are probably not worth salvaging
      Posts.ClosedDate IS NULL AND
      Users.Reputation < 4000 AND -- Posts by trusted users are exempt from editing (that is the theory anyway)
      (
          (
              Posts.PostTypeId IN (1, 3) AND
              Posts.Id <> ALL (
                  SELECT DISTINCT PostId
                  FROM PostHistory
                  WHERE PostId = PostHistory.PostId AND
                        PostHistoryTypeId IN (14, 15)
                  GROUP BY PostId
                  HAVING COUNT(PostHistoryTypeId) % 2 = 1
              )
          ) OR -- Locked posts can't be edited
          (
              Posts.PostTypeId IN (2, 3) AND
              Posts.ParentId <> ALL (
                  SELECT DISTINCT PostId
                  FROM PostHistory
                  WHERE PostId = Posts.ParentId AND
                        PostHistoryTypeId IN (14, 15)
                  GROUP BY PostId
                  HAVING COUNT(PostHistoryTypeId) % 2 = 1
              )
          ) OR
          Posts.PostTypeId IN (4, 5)
      ) AND
      (
          (
              Posts.Title NOT LIKE '%?' AND
              (
                  LOWER(Posts.Title) LIKE 'is %' OR
                  LOWER(Posts.Title) LIKE 'are %' OR
                  LOWER(Posts.Title) LIKE 'do[en ]%' OR
                  LOWER(Posts.Title) LIKE '%wh[aeoy]%' OR
                  LOWER(Posts.Title) LIKE '%how%'
              )
          ) OR
          (
              Posts.Title LIKE '[abcdefghijklmnopqrstuvwxyz]%' OR
              Posts.Title LIKE '%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]([ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrtuvwxyz]%' OR
              Posts.Title LIKE '%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrtuvwxyz])[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]%' OR
              Posts.Title LIKE '%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz],[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]%' OR
              Posts.Title LIKE '% i[ '']%'
          ) OR
          (
              Posts.Body LIKE '[abcdefghijklmnopqrstuvwxyz]%' OR
              Posts.Body LIKE '%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]([ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrtuvwxyz]%' OR
              Posts.Body LIKE '%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrtuvwxyz])[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]%' OR
              Posts.Body LIKE '%[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz],[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]%' OR
              Posts.Body LIKE '% i[ '']%'
          )
      )
ORDER BY Posts.LastActivityDate DESC

Is there any way to speed this up and, more importantly, simplify the query expression? Especially the part where I list the letters of the alphabet in both upper and lower cases, because I think in SQL, [A-Za-z] doesn't give what I want.

Formatting

I really like my SQL queries to be formatted a certain way. For example, if I'm retrieving multiple columns I typically do something like

SELECT
    col1
    ,col2
    ,col3
    ,col4
FROM mytable

I thought it was super ugly at first, but having each on its own line is nice for readability (I don't like having them on the same line as the SELECT either, same reason). Putting the comma at the front of the line seems kinda weird, but I do like that I don't have to try and remember to add commas at the EOL if I grab more columns, which is an error I used to run into all of the time.

I like to format my JOINs a certain way as well, for example

SELECT *
FROM Table1 AS Alias1
     JOIN Table2 AS Alias2
         ON Alias1.Column1 = Alias2.Column3

I find that this makes it much easier to visually separate each table, or CTE, or sub-query, that is being joined. I also find that it makes the conditions by which they're joined much easier to distinguish as well.

Lastly, I find that when combining multiple conditions it is a lot nicer to have the boolean logic (AND/OR) to be at the beginning of the line; then I just look at a single column to see how that row is combined with everything else.

SELECT * FROM myTable
WHERE (COND1
       AND COND2
       AND COND3)
      OR COND4;

Use constants

Right now you have a bunch of magic numbers all over the place, e.g.

Posts.Score > 0
Users.Reputation < 4000
Posts.PostTypeId IN (1, 3) AND
PostHistoryTypeId IN (14, 15)

I personally really like to have constants for values where it makes sense. In this case, I'd probably rewrite those lines to look like this

DECLARE @MinPostScore int = 0
DECLARE @MaxReputation int = 4000
DECLARE @PostTypes TABLE (Value int)
INSERT INTO @PostTypes VALUES (1)
INSERT INTO @PostTypes VALUES (3)
DECLARE @PostHistoryTypes TABLE (Value int)
INSERT INTO @PostHistoryTypes VALUES (14)
INSERT INTO @PostHistoryTypes VALUES (15)

...

Posts.Score > @MinPostScore
Users.Reputation < @MaxReputation
Posts.PostTypeId IN (SELECT Value FROM @PostTypes) AND
PostHistoryTypeId IN (SELECT Value FROM @PostHistoryTypes)

You may also find it helpful to declare values used repeatedly (i.e. '[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]') as variables and insert/concat as needed.

Comments per clause in WHERE

A number of these clauses don't make a lot of sense to a lay reader (mostly because I don't know much about SEDE) - variables instead of magic numbers would help, but maybe a comment describing the query (or replacing it with a function, if performance/SEDE allows it) would help.

Character classes

According to msdn you can use character ranges with LIKE. This would make the last few lines look like this:

Posts.Title LIKE '[a-z]%' OR
Posts.Title LIKE '%[A-Za-z]([A-Za-z]%' OR
Posts.Title LIKE '%[A-Za-z])[A-Za-z]%' OR
Posts.Title LIKE '%[A-Za-z],[A-Za-z]%' OR
Posts.Title LIKE '% i[ '']%'

You can then simplify this even more, like so

Posts.Title LIKE '[a-z]%' OR
Posts.Title LIKE '%[A-Za-z][(),][A-Za-z]%' OR
Posts.Title LIKE '% i[ '']%'

Unfortunately the columns you're comparing use case-sensitive collation (played around using this to figure that out) so you can't just leave out one version. SEDE is run on T-SQL, so it won't support the iLIKE operator either.

Short circuiting

Unfortunately, short circuiting isn't guaranteed in T SQL, so we can't guarantee any optimizations here, however if you carefully examine your conditions you might find ways where you can take advantage of short-circuiting if it were to occur. For example, you might choose to place the "most likely" case first, or use De Morgan's Laws to switch up the order such that each case is mutually exclusive. YMMV.


Simple calculator in C V2 My last question had a lot of views (over a 1000) so I decided to apply what I learned from the previous question and revamp the program. I learnt a lot from posting that question (functions, how to do a menu without using using a lot of printf and learning to not repeat code etc). So here's the updated program:

#include <stdio.h>
#include <stdlib.h>

//Function prototype
void getData(int *,int *);
int addition(int,int);
int subtraction(int,int);
int multiplication(int,int);
int division(int,int);

//Main function
int main()
{
    do//Infinite do - While loop
    {
        int num1,num2,choice;

        printf("\nEnter a number from the list below\n\n"
               "\t1. Addition\n"
               "\t2. Subtraction\n"
               "\t3. Multiplication\n"
               "\t4. Division\n"
               "\t0. Quit program\n");

        printf("\nEnter number: ");
        scanf("%d",&choice);

        if (choice == 0)
        {
            puts("\nExiting program...");
            exit(0);
        }
        else
        {
            getData(&num1,&num2);//Get values for user input

            if(choice == 1)
            {
                printf("\n%d + %d = %d\n",num1,num2,addition(num1,num2));//Output addition
            }
            else if(choice == 2)
            {
                printf("\n%d - %d = %d\n",num1,num2,subtraction(num1,num2));//Output subtraction
            }
            else if(choice == 3)
            {
                printf("\n%d * %d = %d\n",num1,num2,multiplication(num1,num2));//Output multiplication
            }
            else if(choice == 4)
            {
                if(num2 == 0)
                {
                    printf("\nError! Division by zero! Exiting program\n");
                    exit(1);
                }
                else
                {
                    printf("\n%d / %d = %d\n",num1,num2,division(num1,num2));//Output division
                }
            }
            else
            {
                printf("Enter correct number! Exiting program");
                exit(1);
            }
        }
    }while(1);
}

//Get memory address of num1, num2 and store values in this location
void getData(int *num1_ptr,int *num2_ptr)
{
  printf("\nEnter first number: ");
  scanf("%d",num1_ptr);

  printf("\nEnter second number: ");
  scanf("%d",num2_ptr);
}

//Addition
int addition(num1,num2)
{
    return num1 + num2;
}

//Subtraction
int subtraction(int num1,int num2)
{
    return num1 - num2;
}

//Multiplication
int multiplication(int num1,int num2)
{
    return num1 * num2;
}

//Division
int division(int num1,int num2)
{
    return num1 / num2;
}
//End of code

Other reviews already provided some pretty good points, so I'll just mention a few other things and reiterate on some already mentioned ones.

I would advise against function prototypes in this case. Since this is a single source file program, you could declare all functions before main() and avoid the prototypes. The problem with function prototypes is code duplication. If you change one function, at least two updates are mandatory, instead of just one. Avoid this extra maintenance overhead if you can.
For decimal division to work properly, you'll need a to use a floating-point type. My suggestion would be to use double instead of float. The float type is only recommended for storage and memory saving. Most CPUs operate natively on doubles and will convert a float to double when operating on a value. Since you don't have storage constraints, double is the best option.
Don't write noisy and obvious comments. The overall rule about code commenting is to use them to explain why you solved a problem in a given way, or to give any extra information (web links, mathematical formulas, etc) that will aid the understanding of a piece of code. Explaining what a code is doing should not be necessary if the code is well written. Well written code is self explanatory.
Arguably personal preference, but I find it better to properly space expressions and parameter list in function calls.

printf("\n%d + %d = %d\n",num1,num2,addition(num1,num2));

Better spacing:

printf("\n%d + %d = %d\n", num1, num2, addition(num1, num2));


Floor and Ceiling to specified int This relates to Round to specified int but I'm separating these methods out since they're technically different.

Is there a method that already performs a custom floor and ceiling to a specified integer?

// If the interval is 5, 9 --> 5.
public static int FloorTo(this int value, int interval)
{
    var remainder = value % interval;
    return remainder > 0 ? value - remainder : value;
}

// If the interval is 5, 6 --> 10.
public static int CeilingTo(this int value, int interval)
{
    var remainder = value % interval;
    return remainder > 0 ? value + (interval - remainder) : value;
}

Your methods take int parameters but it seems like you only have really thought about positive numbers. This yields some inconsistencies. For example:

(-28).FloorTo(6) == -28
(-28).FloorTo(-6) == -28
(28).FloorTo(-6) == 24

For CeilingTo the results are:

(-28).CeilingTo(6) == -28
(-28).CeilingTo(-6) == -28
(28).CeilingTo(-6) == 18

If you don't want to think about or deal with negative numbers you should either change the signature to accept only uint parameters (which would require the caller to think about what to do in case he's got an int) or you check and throw an ArgumentOutOfRange exception
Just considering positive numbers FloorTo could be changed to

public static int FloorTo(this int value, int interval)
{
   var remainder = value % interval;
   return value - remainder;
}

Less logic to read and verify in your head.


Building query params which append to URL in a browser I was looking for a similar JavaScript method for http_build_query but did not find but create my own and here I am a bit confused to decide the right approach.

Here is the problem. I have an object of params and need to create query string to append with URL with help of encodeURIComponent:

const params = {
    "name": ["alpha &", "beta"],
    "id": ["one", "&two=2"],
    "from": 1533735971000,
    "to": 1533822371147,
    "status": true
};

Now there are two approaches when there is an array of strings are there. I name them pre-encode and post-encode for now.

pre-encode

Apply encodeURIComponent to individual items and then join all items.

const params = {
  "name": ["alpha &", "beta"],
  "id": ["one", "&two=2"],
  "from": 1533735971000,
  "to": 1533822371147,
  "status": true
};

const output = Object.entries(params).map((pair) => {
  const [key, val] = pair;
  if (val instanceof Array && val.length > 0) {
    let pre_encode = val.map(encodeURIComponent).join(',');
    console.log({pre_encode});
    const pre_qs = `${encodeURIComponent(key)}=${pre_encode}`;
    console.log({pre_qs})
    return pre_qs;
  } else {
    return pair.map(encodeURIComponent).join('=');
  }

}).join('&');

console.log({output});

Output

  "name=alpha%20%26,beta&id=one,%26two%3D2&from=1533735971000&to=1533822371147&status=true"

post-encode

First, join all items then apply encodeURIComponent.

const params = {
  "name": ["alpha &", "beta"],
  "id": ["one", "&two=2"],
  "from": 1533735971000,
  "to": 1533822371147,
  "status": true
};

const output = Object.entries(params).map((pair) => {
  const [key, val] = pair;
  if (val instanceof Array && val.length > 0) {
    let post_encode = encodeURIComponent(val.join(','));
    console.log({post_encode});
    const post_qs = `${encodeURIComponent(key)}=${post_encode}`;
    console.log({post_qs})
    return post_qs;
  } else {
    return pair.map(encodeURIComponent).join('=');
  }

}).join('&');

console.log({output});

Output

  "name=alpha%20%26%2Cbeta&id=one%2C%26two%3D2&from=1533735971000&to=1533822371147&status=true"

Which approach is the more efficient and right and how to handle null and undefined values?

Your main question

I have trouble understanding it, because:

minor reason:
(unless I'm missing something obvious) whatever way you encode the array parts, the resulting query string is quite valid in both cases;
so the only possible criterion for choosing a method rather than the other one would be to find which one is faster... and this sounds a bit overkill!
major reason:
you initially referred to the PHP http_build_query() function, so I'd expect you work the same, i.e. for a given key: [val_1, val_2]:

you're currently returning key=val_1,val_2
while it should be key=val_1&key=val_2

To match the latter case, I'd suggest something like this:

const output = Object.entries(params).map(
  (pair) => {
    let [key, val] = pair;
    // normalize val to always an array
    val = (val instanceof Array) ? val : [val];
    // encode key only once
    key = encodeURIComponent(key);
    // then make a different query string for each val member
    return val.map(
      (member) => `${key}=${encodeURIComponent(member)}`
    ).join('&');
  }
).join('&');

Your complementary question

To implement null or undefined, first you have to decide how they must be managed: they might be simply omitted, or included with no value (its often useful to have query params where only its presence/absence is meaningful for the underlying application).

Anyway you may simply add any of these capabilities; instead of

    return val.map(
      (member) => `${key}=${encodeURIComponent(member)}`
    ).join('&');

you can include null/undefined params simply with:

return val.map(
  (member) => (member ? `${key}=${encodeURIComponent(member)}` : `${key}`)
).join('&');

(NOTE: it's up to you to enhance this with more precise tests if desired, in order to keep 0 as key=0, or '' as key=)

or you can omit them with a slightly different method:

return val.reduce(
  (result, member) => {
    if (member) {
      result.push(`${key}=${encodeURIComponent(member)}`);
    }
    return result;
  },
  []
).join('&');