问:
EF Core queries all columns in SQL when mapping to object in Select
While trying to organize some data access code using EF Core I noticed that the generated queries were worse than before, they now queried columns that were not needed. The basic query is just selecting from one table and mapping a subset of columns to a DTO. But after rewriting it now all columns are fetched, not just the ones in the DTO.

I created a minimal example with some queries that show the problem:

```<code>ctx.Items.ToList();
// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i

ctx.Items.Select(x => new
{
  Id = x.Id,
  Property1 = x.Property1
}
).ToList();
// SELECT i."Id", i."Property1" FROM "Items" AS i

ctx.Items.Select(x => new MinimalItem
{
  Id = x.Id,
  Property1 = x.Property1
}
).ToList();
// SELECT i."Id", i."Property1" FROM "Items" AS i

ctx.Items.Select(
  x => x.MapToMinimalItem()
).ToList();
// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i

ctx.Items.Select(
  x => new MinimalItem(x)
).ToList();

// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i
</code>```

The objects are defined like this:

```<code>  public class Item
  {
    public int Id { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }

  }

  public class MinimalItem
  {
    public MinimalItem() { }

    public MinimalItem(Item source)
    {
      Id = source.Id;
      Property1 = source.Property1;
    }
    public int Id { get; set; }
    public string Property1 { get; set; }
  }

  public static class ItemExtensionMethods
  {
    public static MinimalItem MapToMinimalItem(this Item source)
    {
      return new MinimalItem
      {
        Id = source.Id,
        Property1 = source.Property1
      };
    }
  }
</code>```

The first query queries all columns as intended, and the second query with an anonymous object only queries the selected queries, that works all fine. Using my MinimalItem DTO also works as long as it is created directly in the Select method. But the last two queries fetch all columns even though they do exactly the same thing as the third query, just moved to a constructor or an extension method, respectively.

Obviously EF Core can't follow this code and determine that it only needs the two columns if I move it out of the Select method. But I'd really like to do that to be able to reuse the mapping code, and make the actual query code easier to read. How can I extract this kind of straightforward mapping code without making EF Core inefficiently fetching all columns all the time?

答:
This is fundamental problem with IQueryable from the very beginning, with no out of the box solution after so many years.

The problem is that IQueryable translation and code encapsulation/reusability are mutually exclusive. IQueryable translation is based on knowledge in advance, which means the query processor must be able to "see" the actual code, and then translate the "known" methods/properties. But the content of the custom methods / calculable  properties is not visible at runtime, so query processors usually fail, or in limited cases where they support "client evaluation" (EF Core does that only for final projections) they generate inefficient translation which retrieves much more data than needed like in your examples.

To recap, neither C# compiler nor BCL helps solving this "core concern". Some 3rd party libraries are trying to address it in different level of degree - LinqKit, NeinLinq and similar. The problem with them is that they require refactoring your existing code additionally to calling a special method like AsExpandable(), ToInjectable() etc.

Recently I found a little gem called DelegateDecompiler, which uses another package called Mono.Reflection.Core to decompile method body to its lambda representation.

Using it is quite easy. All you need after installing it is to mark your custom methods / computed properties with custom provided [Computed] or [Decompile] attributes (just make sure you use expression style implementation and not code blocks), and call Decompile() or DecompileAsync() custom extension method somewhere in the IQueryable chain. It doesn't work with constructors, but all other constructs are supported.

For instance, taking your extension method example:

```<code>public static class ItemExtensionMethods
{
    [Decompile] // <--
    public static MinimalItem MapToMinimalItem(this Item source)
    {
        return new MinimalItem
        {
            Id = source.Id,
            Property1 = source.Property1
        };
    }
}
</code>```

(Note: it supports other ways of telling which methods to decompile, for instance all methods/properties of specific class etc.)

and now

```<code>ctx.Items.Decompile()
    .Select(x => x.MapToMinimalItem())
    .ToList();
</code>```

produces

```<code>// SELECT i."Id", i."Property1" FROM "Items" AS i
</code>```

The only problem with this approach (and other 3rd party libraries) is the need of calling custom extension method Decompile, in order to wrap the queryable with custom provider just to be able to preprocess the final query expression.

It would have been nice if EF Core allow plugging custom query expression preprocessor in its LINQ query processing pipeline, thus eliminating the need of calling custom method in each query, which could easily be forgotten, and also custom query providers does not play well with EF Core specific extensions like AsTracking, AsNoTracking, Include/ ThenInclude, so it should really be called after them etc.

Update (EF Core 7.0+):

EF Core 7.0 finally added Interception to modify the LINQ expression tree capability, so now the plumbing code is reduced to

using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace Microsoft.EntityFrameworkCore
{
    public static class DelegateDecompilerDbContextOptionsBuilderExtensions
    {
        public static DbContextOptionsBuilder AddDelegateDecompiler(this DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.AddInterceptors(new DelegateDecompilerQueryPreprocessor());
    }
}

namespace Microsoft.EntityFrameworkCore.Query
{
    using System.Linq.Expressions;
    using DelegateDecompiler;
    public class DelegateDecompilerQueryPreprocessor : IQueryExpressionInterceptor
    {
        Expression IQueryExpressionInterceptor.QueryCompilationStarting(Expression queryExpression, QueryExpressionEventData eventData)
            => DecompileExpressionVisitor.Decompile(queryExpression);
    }
}
```
<hr />
<p><strong>Original:</strong></p>
<p>Currently there is an open issue Please open the query translation pipeline for extension #19748 where I'm trying to convince the team to add an easy way to add expression preprocessor. You can read the discussion and vote up.</p>
<p>Until then, here is my solution for EF Core 3.1:</p>
```using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore
{
    public static partial class CustomDbContextOptionsExtensions
    {
        public static DbContextOptionsBuilder AddQueryPreprocessor(this DbContextOptionsBuilder optionsBuilder, IQueryPreprocessor processor)
        {
            var option = optionsBuilder.Options.FindExtension<CustomOptionsExtension>()?.Clone() ?? new CustomOptionsExtension();
            if (option.Processors.Count == 0)
                optionsBuilder.ReplaceService<IQueryTranslationPreprocessorFactory, CustomQueryTranslationPreprocessorFactory>();
            else
                option.Processors.Remove(processor);
            option.Processors.Add(processor);
            ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(option);
            return optionsBuilder;
        }
    }
}

namespace Microsoft.EntityFrameworkCore.Infrastructure
{
    public class CustomOptionsExtension : IDbContextOptionsExtension
    {
        public CustomOptionsExtension() { }
        private CustomOptionsExtension(CustomOptionsExtension copyFrom) => Processors = copyFrom.Processors.ToList();
        public CustomOptionsExtension Clone() => new CustomOptionsExtension(this);
        public List<IQueryPreprocessor> Processors { get; } = new List<IQueryPreprocessor>();
        ExtensionInfo info;
        public DbContextOptionsExtensionInfo Info => info ?? (info = new ExtensionInfo(this));
        public void Validate(IDbContextOptions options) { }
        public void ApplyServices(IServiceCollection services)
            => services.AddSingleton<IEnumerable<IQueryPreprocessor>>(Processors);
        private sealed class ExtensionInfo : DbContextOptionsExtensionInfo
        {
            public ExtensionInfo(CustomOptionsExtension extension) : base(extension) { }
            new private CustomOptionsExtension Extension => (CustomOptionsExtension)base.Extension;
            public override bool IsDatabaseProvider => false;
            public override string LogFragment => string.Empty;
            public override void PopulateDebugInfo(IDictionary<string, string> debugInfo) { }
            public override long GetServiceProviderHashCode() => Extension.Processors.Count;
        }
    }
}

namespace Microsoft.EntityFrameworkCore.Query
{
    public interface IQueryPreprocessor
    {
        Expression Process(Expression query);
    }

    public class CustomQueryTranslationPreprocessor : RelationalQueryTranslationPreprocessor
    {
        public CustomQueryTranslationPreprocessor(QueryTranslationPreprocessorDependencies dependencies, RelationalQueryTranslationPreprocessorDependencies relationalDependencies, IEnumerable<IQueryPreprocessor> processors, QueryCompilationContext queryCompilationContext)
            : base(dependencies, relationalDependencies, queryCompilationContext) => Processors = processors;
        protected IEnumerable<IQueryPreprocessor> Processors { get; }
        public override Expression Process(Expression query)
        {
            foreach (var processor in Processors)
                query = processor.Process(query);
            return base.Process(query);
        }
    }

    public class CustomQueryTranslationPreprocessorFactory : IQueryTranslationPreprocessorFactory
    {
        public CustomQueryTranslationPreprocessorFactory(QueryTranslationPreprocessorDependencies dependencies, RelationalQueryTranslationPreprocessorDependencies relationalDependencies, IEnumerable<IQueryPreprocessor> processors)
        {
            Dependencies = dependencies;
            RelationalDependencies = relationalDependencies;
            Processors = processors;
        }
        protected QueryTranslationPreprocessorDependencies Dependencies { get; }
        protected RelationalQueryTranslationPreprocessorDependencies RelationalDependencies { get; }
        protected IEnumerable<IQueryPreprocessor> Processors { get; }
        public QueryTranslationPreprocessor Create(QueryCompilationContext queryCompilationContext)
            => new CustomQueryTranslationPreprocessor(Dependencies, RelationalDependencies, Processors, queryCompilationContext);
    }
}
```
<p>You don't need to understand that code. Most (if not all) of it is a boilerplate plumbing code to support the currently missing <code>IQueryPreprocessor</code> and <code>AddQueryPreprocesor</code> (similar to recently added interceptors). I'll update it if EF Core adds that functionality in the future.</p>
<p>Now you can use it to plug the <code>DelegateDecompiler</code> into EF Core:</p>
```using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query;
using DelegateDecompiler;

namespace Microsoft.EntityFrameworkCore
{
    public static class DelegateDecompilerDbContextOptionsExtensions
    {
        public static DbContextOptionsBuilder AddDelegateDecompiler(this DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.AddQueryPreprocessor(new DelegateDecompilerQueryPreprocessor());
    }
}

namespace Microsoft.EntityFrameworkCore.Query
{
    public class DelegateDecompilerQueryPreprocessor : IQueryPreprocessor
    {
        public Expression Process(Expression query) => DecompileExpressionVisitor.Decompile(query);
    }
}
```
<p>A lot of code just to be able to call</p>
```DecompileExpressionVisitor.Decompile(query)
```
<p>before EF Core processing, but it is what it is.</p>
<hr />
<p>Now all you need is to call</p>
```optionsBuilder.AddDelegateDecompiler();
```
<p>in your derived context <code>OnConfiguring</code> override, and all your EF Core LINQ queries will be preprocessed and decompiled bodies injected.</p>
<p>With you examples</p>
```ctx.Items.Select(x => x.MapToMinimalItem())
```
<p>will automatically be converted to</p>
```ctx.Items.Select(x => new
{
    Id = x.Id,
    Property1 = x.Property1
}
```
<p>thus translated by EF Core to</p>
```// SELECT i."Id", i."Property1" FROM "Items" AS I
```
<p>which was the goal.</p>
<p>Additionally, composing over projection also works, so the following query</p>
```ctx.Items.Decompile()
    .Select(x => x.MapToMinimalItem())
    .ToList();
```0

originally would have generated runtime exception, but now translates and runs successfully.

Body:
While trying to organize some data access code using EF Core I noticed that the generated queries were worse than before, they now queried columns that were not needed. The basic query is just selecting from one table and mapping a subset of columns to a DTO. But after rewriting it now all columns are fetched, not just the ones in the DTO.

I created a minimal example with some queries that show the problem:

ctx.Items.ToList();
// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i

ctx.Items.Select(x => new
{
  Id = x.Id,
  Property1 = x.Property1
}
).ToList();
// SELECT i."Id", i."Property1" FROM "Items" AS i

ctx.Items.Select(x => new MinimalItem
{
  Id = x.Id,
  Property1 = x.Property1
}
).ToList();
// SELECT i."Id", i."Property1" FROM "Items" AS i

ctx.Items.Select(
  x => x.MapToMinimalItem()
).ToList();
// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i

ctx.Items.Select(
  x => new MinimalItem(x)
).ToList();

// SELECT i."Id", i."Property1", i."Property2", i."Property3" FROM "Items" AS i

The objects are defined like this:

  public class Item
  {
    public int Id { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }

  }

  public class MinimalItem
  {
    public MinimalItem() { }

    public MinimalItem(Item source)
    {
      Id = source.Id;
      Property1 = source.Property1;
    }
    public int Id { get; set; }
    public string Property1 { get; set; }
  }

  public static class ItemExtensionMethods
  {
    public static MinimalItem MapToMinimalItem(this Item source)
    {
      return new MinimalItem
      {
        Id = source.Id,
        Property1 = source.Property1
      };
    }
  }

The first query queries all columns as intended, and the second query with an anonymous object only queries the selected queries, that works all fine. Using my MinimalItem DTO also works as long as it is created directly in the Select method. But the last two queries fetch all columns even though they do exactly the same thing as the third query, just moved to a constructor or an extension method, respectively.

Obviously EF Core can't follow this code and determine that it only needs the two columns if I move it out of the Select method. But I'd really like to do that to be able to reuse the mapping code, and make the actual query code easier to read. How can I extract this kind of straightforward mapping code without making EF Core inefficiently fetching all columns all the time?

Body:
This is fundamental problem with IQueryable from the very beginning, with no out of the box solution after so many years.

The problem is that IQueryable translation and code encapsulation/reusability are mutually exclusive. IQueryable translation is based on knowledge in advance, which means the query processor must be able to "see" the actual code, and then translate the "known" methods/properties. But the content of the custom methods / calculable  properties is not visible at runtime, so query processors usually fail, or in limited cases where they support "client evaluation" (EF Core does that only for final projections) they generate inefficient translation which retrieves much more data than needed like in your examples.

To recap, neither C# compiler nor BCL helps solving this "core concern". Some 3rd party libraries are trying to address it in different level of degree - LinqKit, NeinLinq and similar. The problem with them is that they require refactoring your existing code additionally to calling a special method like AsExpandable(), ToInjectable() etc.

Recently I found a little gem called DelegateDecompiler, which uses another package called Mono.Reflection.Core to decompile method body to its lambda representation.

Using it is quite easy. All you need after installing it is to mark your custom methods / computed properties with custom provided [Computed] or [Decompile] attributes (just make sure you use expression style implementation and not code blocks), and call Decompile() or DecompileAsync() custom extension method somewhere in the IQueryable chain. It doesn't work with constructors, but all other constructs are supported.

For instance, taking your extension method example:

public static class ItemExtensionMethods
{
    [Decompile] // <--
    public static MinimalItem MapToMinimalItem(this Item source)
    {
        return new MinimalItem
        {
            Id = source.Id,
            Property1 = source.Property1
        };
    }
}

(Note: it supports other ways of telling which methods to decompile, for instance all methods/properties of specific class etc.)

and now

ctx.Items.Decompile()
    .Select(x => x.MapToMinimalItem())
    .ToList();

produces

// SELECT i."Id", i."Property1" FROM "Items" AS i

The only problem with this approach (and other 3rd party libraries) is the need of calling custom extension method Decompile, in order to wrap the queryable with custom provider just to be able to preprocess the final query expression.

It would have been nice if EF Core allow plugging custom query expression preprocessor in its LINQ query processing pipeline, thus eliminating the need of calling custom method in each query, which could easily be forgotten, and also custom query providers does not play well with EF Core specific extensions like AsTracking, AsNoTracking, Include/ ThenInclude, so it should really be called after them etc.

Update (EF Core 7.0+):

EF Core 7.0 finally added Interception to modify the LINQ expression tree capability, so now the plumbing code is reduced to

using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace Microsoft.EntityFrameworkCore
{
    public static class DelegateDecompilerDbContextOptionsBuilderExtensions
    {
        public static DbContextOptionsBuilder AddDelegateDecompiler(this DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.AddInterceptors(new DelegateDecompilerQueryPreprocessor());
    }
}

namespace Microsoft.EntityFrameworkCore.Query
{
    using System.Linq.Expressions;
    using DelegateDecompiler;
    public class DelegateDecompilerQueryPreprocessor : IQueryExpressionInterceptor
    {
        Expression IQueryExpressionInterceptor.QueryCompilationStarting(Expression queryExpression, QueryExpressionEventData eventData)
            => DecompileExpressionVisitor.Decompile(queryExpression);
    }
}

Original:

Currently there is an open issue Please open the query translation pipeline for extension #19748 where I'm trying to convince the team to add an easy way to add expression preprocessor. You can read the discussion and vote up.

Until then, here is my solution for EF Core 3.1:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.EntityFrameworkCore
{
    public static partial class CustomDbContextOptionsExtensions
    {
        public static DbContextOptionsBuilder AddQueryPreprocessor(this DbContextOptionsBuilder optionsBuilder, IQueryPreprocessor processor)
        {
            var option = optionsBuilder.Options.FindExtension<CustomOptionsExtension>()?.Clone() ?? new CustomOptionsExtension();
            if (option.Processors.Count == 0)
                optionsBuilder.ReplaceService<IQueryTranslationPreprocessorFactory, CustomQueryTranslationPreprocessorFactory>();
            else
                option.Processors.Remove(processor);
            option.Processors.Add(processor);
            ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(option);
            return optionsBuilder;
        }
    }
}

namespace Microsoft.EntityFrameworkCore.Infrastructure
{
    public class CustomOptionsExtension : IDbContextOptionsExtension
    {
        public CustomOptionsExtension() { }
        private CustomOptionsExtension(CustomOptionsExtension copyFrom) => Processors = copyFrom.Processors.ToList();
        public CustomOptionsExtension Clone() => new CustomOptionsExtension(this);
        public List<IQueryPreprocessor> Processors { get; } = new List<IQueryPreprocessor>();
        ExtensionInfo info;
        public DbContextOptionsExtensionInfo Info => info ?? (info = new ExtensionInfo(this));
        public void Validate(IDbContextOptions options) { }
        public void ApplyServices(IServiceCollection services)
            => services.AddSingleton<IEnumerable<IQueryPreprocessor>>(Processors);
        private sealed class ExtensionInfo : DbContextOptionsExtensionInfo
        {
            public ExtensionInfo(CustomOptionsExtension extension) : base(extension) { }
            new private CustomOptionsExtension Extension => (CustomOptionsExtension)base.Extension;
            public override bool IsDatabaseProvider => false;
            public override string LogFragment => string.Empty;
            public override void PopulateDebugInfo(IDictionary<string, string> debugInfo) { }
            public override long GetServiceProviderHashCode() => Extension.Processors.Count;
        }
    }
}

namespace Microsoft.EntityFrameworkCore.Query
{
    public interface IQueryPreprocessor
    {
        Expression Process(Expression query);
    }

    public class CustomQueryTranslationPreprocessor : RelationalQueryTranslationPreprocessor
    {
        public CustomQueryTranslationPreprocessor(QueryTranslationPreprocessorDependencies dependencies, RelationalQueryTranslationPreprocessorDependencies relationalDependencies, IEnumerable<IQueryPreprocessor> processors, QueryCompilationContext queryCompilationContext)
            : base(dependencies, relationalDependencies, queryCompilationContext) => Processors = processors;
        protected IEnumerable<IQueryPreprocessor> Processors { get; }
        public override Expression Process(Expression query)
        {
            foreach (var processor in Processors)
                query = processor.Process(query);
            return base.Process(query);
        }
    }

    public class CustomQueryTranslationPreprocessorFactory : IQueryTranslationPreprocessorFactory
    {
        public CustomQueryTranslationPreprocessorFactory(QueryTranslationPreprocessorDependencies dependencies, RelationalQueryTranslationPreprocessorDependencies relationalDependencies, IEnumerable<IQueryPreprocessor> processors)
        {
            Dependencies = dependencies;
            RelationalDependencies = relationalDependencies;
            Processors = processors;
        }
        protected QueryTranslationPreprocessorDependencies Dependencies { get; }
        protected RelationalQueryTranslationPreprocessorDependencies RelationalDependencies { get; }
        protected IEnumerable<IQueryPreprocessor> Processors { get; }
        public QueryTranslationPreprocessor Create(QueryCompilationContext queryCompilationContext)
            => new CustomQueryTranslationPreprocessor(Dependencies, RelationalDependencies, Processors, queryCompilationContext);
    }
}

You don't need to understand that code. Most (if not all) of it is a boilerplate plumbing code to support the currently missing IQueryPreprocessor and AddQueryPreprocesor (similar to recently added interceptors). I'll update it if EF Core adds that functionality in the future.

Now you can use it to plug the DelegateDecompiler into EF Core:

using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore.Query;
using DelegateDecompiler;

namespace Microsoft.EntityFrameworkCore
{
    public static class DelegateDecompilerDbContextOptionsExtensions
    {
        public static DbContextOptionsBuilder AddDelegateDecompiler(this DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.AddQueryPreprocessor(new DelegateDecompilerQueryPreprocessor());
    }
}

namespace Microsoft.EntityFrameworkCore.Query
{
    public class DelegateDecompilerQueryPreprocessor : IQueryPreprocessor
    {
        public Expression Process(Expression query) => DecompileExpressionVisitor.Decompile(query);
    }
}

A lot of code just to be able to call

DecompileExpressionVisitor.Decompile(query)

before EF Core processing, but it is what it is.

Now all you need is to call

optionsBuilder.AddDelegateDecompiler();

in your derived context OnConfiguring override, and all your EF Core LINQ queries will be preprocessed and decompiled bodies injected.

With you examples

ctx.Items.Select(x => x.MapToMinimalItem())

will automatically be converted to

ctx.Items.Select(x => new
{
    Id = x.Id,
    Property1 = x.Property1
}

thus translated by EF Core to

// SELECT i."Id", i."Property1" FROM "Items" AS I

which was the goal.

Additionally, composing over projection also works, so the following query

ctx.Items
    .Select(x => x.MapToMinimalItem())
    .Where(x => x.Property1 == "abc")
    .ToList();

originally would have generated runtime exception, but now translates and runs successfully.


来源: stackexchange

问:
How do I get All actors that haven't appeared in R rated films

I want to get All actors  that haven't appeared in R rated films

It was easy to do with sub-queries

```<code>select *
from actor
where actor_id not in
  (select actor_id
   from actor
     inner join film_actor using (actor_id)
     inner join film using (film_id)
   where rating = 'R');
</code>```

I am unable to do it with joins -

I have done this till now -

```<code>select distinct(a.actor_id), a.first_name, a.last_name
from actor as a
  left join
    (film_actor as fa
       inner join film as f
         on fa.film_id = f.film_id)
  on a.actor_id = fa.actor_id
  where f.rating = 'R'
  order by actor_id
</code>```

In the above query I get all the actors who have worked in R rated films, but now I am not sure how to get the non R rated films

Can someone help me do this with joins?

答:
So, if you know how to find all actors in R-rated films, then SQL subtraction is a good solution.
Select all actors
minus
Select all actors in R-rated files.</p>

ContentLicense: CC BY-SA 4.0

Body:
I want to get All actors  that haven't appeared in R rated films

It was easy to do with sub-queries

select *
from actor
where actor_id not in
  (select actor_id
   from actor
     inner join film_actor using (actor_id)
     inner join film using (film_id)
   where rating = 'R');

I am unable to do it with joins -

I have done this till now -

select distinct(a.actor_id), a.first_name, a.last_name
from actor as a
  left join
    (film_actor as fa
       inner join film as f
         on fa.film_id = f.film_id)
  on a.actor_id = fa.actor_id
  where f.rating = 'R'
  order by actor_id

In the above query I get all the actors who have worked in R rated films, but now I am not sure how to get the non R rated films

Can someone help me do this with joins?


问:
"TypeError: undefined is not an object (evaluating '_expo.default.Font')]" facing this error when asynchronously loading the font
This is my error:

[Unhandled promise rejection: TypeError: undefined is not an object
  (evaluating '_expo.default.Font')] * app\views\Login.js:33:15 in
  componentWillMount$

Initially I was getting an error like:

fontFamily "Roboto_medium" is not a system font and has not been
  loaded through Font.loadAsync. If you intended to use a system font,
  make sure you typed the name correctly and that it is supported by
  your device operating system.

So i used load async to load the font, but started getting the above error.

```<code>import React, { Component } from "react";
import { Alert, AsyncStorage, StyleSheet, Text } from "react-native";
import {Container,Header,Content,Card,CardItem,Body,Form,Input,Button,Item
} from "native-base";
import { AppHeader } from "../sections/Header";
import Expo from "expo";
import {Font} from 'expo';
export class Login extends Component {
  static navigationOptions = {
    header: null
  };

  constructor(props) {
    super(props);
    this.state = {
      username: "",
      password: "",
      isReady: false
    };
  }

  async componentWillMount() {
    await Expo.Font.loadAsync({
      Roboto: require("native-base/Fonts/Roboto.ttf"),
      Roboto_medium: require("native-base/Fonts/Roboto_medium.ttf")
    });
    this.setState({ isReady: true });
  }

  render() {
    if (!this.state.isReady) {
      return <Expo.AppLoading />;
    }
    return (
      <Container>
        <AppHeader />
      </Container>
    );
  }
}
</code>```

[Unhandled promise rejection: TypeError: undefined is not an object
  (evaluating '_expo.default.Font')]
  * app\views\Login.js:33:15 in componentWillMount$
  - node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
  - node_modules\regenerator-runtime\runtime.js:271:30 in invoke
  - node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
  - node_modules\regenerator-runtime\runtime.js:135:28 in invoke
  - node_modules\regenerator-runtime\runtime.js:170:17 in
  - node_modules\promise\setimmediate\core.js:45:7 in tryCallTwo
  - node_modules\promise\setimmediate\core.js:200:23 in doResolve

This is the error that i am facing

答: Also use import * as Font from 'expo-font' in the newer versions of Expo instead of importing it from 'expo'.

Body:
This is my error:

[Unhandled promise rejection: TypeError: undefined is not an object
  (evaluating '_expo.default.Font')] * app\views\Login.js:33:15 in
  componentWillMount$

Initially I was getting an error like:

fontFamily "Roboto_medium" is not a system font and has not been
  loaded through Font.loadAsync. If you intended to use a system font,
  make sure you typed the name correctly and that it is supported by
  your device operating system.

So i used load async to load the font, but started getting the above error.

import React, { Component } from "react";
import { Alert, AsyncStorage, StyleSheet, Text } from "react-native";
import {Container,Header,Content,Card,CardItem,Body,Form,Input,Button,Item
} from "native-base";
import { AppHeader } from "../sections/Header";
import Expo from "expo";
import {Font} from 'expo';
export class Login extends Component {
  static navigationOptions = {
    header: null
  };

  constructor(props) {
    super(props);
    this.state = {
      username: "",
      password: "",
      isReady: false
    };
  }

  async componentWillMount() {
    await Expo.Font.loadAsync({
      Roboto: require("native-base/Fonts/Roboto.ttf"),
      Roboto_medium: require("native-base/Fonts/Roboto_medium.ttf")
    });
    this.setState({ isReady: true });
  }

  render() {
    if (!this.state.isReady) {
      return <Expo.AppLoading />;
    }
    return (
      <Container>
        <AppHeader />
      </Container>
    );
  }
}

[Unhandled promise rejection: TypeError: undefined is not an object
  (evaluating '_expo.default.Font')]
  * app\views\Login.js:33:15 in componentWillMount$
  - node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
  - node_modules\regenerator-runtime\runtime.js:271:30 in invoke
  - node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch
  - node_modules\regenerator-runtime\runtime.js:135:28 in invoke
  - node_modules\regenerator-runtime\runtime.js:170:17 in
  - node_modules\promise\setimmediate\core.js:45:7 in tryCallTwo
  - node_modules\promise\setimmediate\core.js:200:23 in doResolve

This is the error that i am facing


问:
Custom fields in Laravel scoping
I have an eloquent model with a relationship and a scope for that relationship

```<code><?php

namespace App;
...

class Shift extends Model
{
    public function profession()
    {
        return $this->hasOne(Profession::class, 'id', 'profession');
    }
    ...
    public function scopeWithProfession($query)
    {
        $query->with('profession');
    }
...
}
</code>```

Then, in my controller, I just call this scope as follows, and it gives me the fields in the professions table:

```<code>public function show(Shift $shift)
{
    return Shift::where('id', $shift->id)->withProfession()->first();
}
</code>```

Now, my profession item has a self-referencing foreign key that assigns an optional parent object to a profession instance

```<code>Schema::create('professions', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('color');
        $table->unsignedBigInteger('parent_id')->nullable();
        $table->timestamps();

        $table->foreign('parent_id')
            ->references('id')
            ->on('professions');
    });
</code>```

What I want to achieve, is in the call to the withProfession scope, I also want to return a field that references the name of the parent object as an extra to the normal fields in the profession table. This means I essentially need to "add" a custom field to the withProfession scope. How do I do this?

答:
there is a simple solution but it load the whole parent:
the base idea is to define a parentProfession relation in Profession Model
then load it in with inside your scope ...

```<code>class Profession extends Model
{
    public function parentProfession()
    {
        return $this->hasOne(Profession::class, 'parent_id');
    }

}

class Shift extends Model
{
    public function profession()
    {
        return $this->hasOne(Profession::class, 'id', 'profession');
    }
    ...
    public function scopeWithProfession($query)
    {
        $query->with('profession.parentProfession');
    }
...
}
</code>```

this should load the Profession with it's parent ...
if you has to get only the name, then you have to a chain of join with 'addSelect'

Title: Custom fields in Laravel scoping

Body:
I have an eloquent model with a relationship and a scope for that relationship

<?php

namespace App;
...

class Shift extends Model
{
    public function profession()
    {
        return $this->hasOne(Profession::class, 'id', 'profession');
    }
    ...
    public function scopeWithProfession($query)
    {
        $query->with('profession');
    }
...
}

Then, in my controller, I just call this scope as follows, and it gives me the fields in the professions table:

public function show(Shift $shift)
{
    return Shift::where('id', $shift->id)->withProfession()->first();
}

Now, my profession item has a self-referencing foreign key that assigns an optional parent object to a profession instance

Schema::create('professions', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('color');
        $table->unsignedBigInteger('parent_id')->nullable();
        $table->timestamps();

        $table->foreign('parent_id')
            ->references('id')
            ->on('professions');
    });

What I want to achieve, is in the call to the withProfession scope, I also want to return a field that references the name of the parent object as an extra to the normal fields in the profession table. This means I essentially need to "add" a custom field to the withProfession scope. How do I do this?

Body:
there is a simple solution but it load the whole parent:
the base idea is to define a parentProfession relation in Profession Model
then load it in with inside your scope ...

class Profession extends Model
{
    public function parentProfession()
    {
        return $this->hasOne(Profession::class, 'parent_id');
    }

}

class Shift extends Model
{
    public function profession()
    {
        return $this->hasOne(Profession::class, 'id', 'profession');
    }
    ...
    public function scopeWithProfession($query)
    {
        $query->with('profession.parentProfession');
    }
...
}

this should load the Profession with it's parent ...
if you has to get only the name, then you have to a chain of join with 'addSelect'


问:
Can the config crate merge environment variables into a subsection of a hierarchical config file?
I am using the Config crate in Rust, and would like to use environment variables to set keys inside a section of the config. The end goal is to override application settings from a docker compose file, or docker command line, using the environment.

If my config was the following, could I use a specifically crafted environment variable to set database.echo ?

(code blurb below is taken from this example)

```<code>debug = true

[database]
echo = true
</code>```

The example code to configure this using the environment variables illustrates only to set keys at the top level. Wondering how to extend this. The .set() takes a hierarchical key, so I'm hopeful that there's a way to encode the path in the env variable name.

答:
Answering my own question.

I just noticed that the Environment code accepts a custom separator which will get replaced with . (dot).

So one can set the separator to something like _XX_ and that would get mapped to a ".". Setting DATABASE_XX_ECHO=true, for instance would then change the database.echo key.

Body:
I am using the Config crate in Rust, and would like to use environment variables to set keys inside a section of the config. The end goal is to override application settings from a docker compose file, or docker command line, using the environment.

If my config was the following, could I use a specifically crafted environment variable to set database.echo ?

(code blurb below is taken from this example)

debug = true

[database]
echo = true

The example code to configure this using the environment variables illustrates only to set keys at the top level. Wondering how to extend this. The .set() takes a hierarchical key, so I'm hopeful that there's a way to encode the path in the env variable name.


问:
Vue.js: Search through nested arrays
I have been working on a FAQ page that has categories and once you have selected a category, it shows questions users have asked. You can then select a question to display the answer. There are 7 categories and each has an array named "questionList" that has various amounts of questions. I am creating a search bar that will filter through ALL questions in each category and display the question and answer that matches the search. Right now the filtered list only displays the first categories questions....

Here is an example of how my data is setup

```<code>categoryList : [
           {
             category: 'Category Title',
             questionList: [
                      {
                         question: 'Question',
                         answer: '<p>copy goes here</p>'
                       }, ...
                     ]
            },
            {
               category: 'Next Title',
               questionList: [
                       {
                         question: 'Question',
                         answer: '<p>copy goes here</p>'
                       },
                     ]
             }, ...
</code>```

I was looking at another stackoverflow solution and I really could not figure out how to have it function with my multiple nested arrays. Here is the html:

```<code><div class="filtered" v-for="qa in filteredList">
     <div class="filter-container">
          <h3>{{ qa.question }}</h3>
          <div v-html="qa.answer"></div>
     </div>
</div>
</code>```

and my computed function:

```<code>filteredList() {
      for (i = 0; i < this.categoryList.length; i++) {

            var list =  this.categoryList[i];

            for (j = 0; j < list.questionList.length; j++ ) {

                    return list.questionList.filter(qa => {
                                 return qa.question.toLowerCase().includes(this.search.toLowerCase());
                        })
                   }
              }
          }
</code>```

I'm not sure if I am even close to the correct solution or waaaay off here... please help!

答:
This is the code:

data()
{
  return {
    searchTerm: '',
  }
},
computed:
{
  allQuestions()
  {
    // un-nest all questions as a flat array
    return this.categoryList
      .map(category => category.questionList)
      .reduce((acc, item) => acc.concat(item), []);
  }
  filteredList()
  {
    const term = this.searchTerm.toLowerCase();
    if(!term) return this.allQuestions;
    // only questions or answers which contain the term will be returned
    return this.allQuestions.filter(qa => qa.question.toLowerCase().indexOf(term) >= 0
      || qa.answer.toLowerCase().indexOf(term) >= 0);
  }
}
```

Title: Vue.js: Search through nested arrays

Body:
I have been working on a FAQ page that has categories and once you have selected a category, it shows questions users have asked. You can then select a question to display the answer. There are 7 categories and each has an array named "questionList" that has various amounts of questions. I am creating a search bar that will filter through ALL questions in each category and display the question and answer that matches the search. Right now the filtered list only displays the first categories questions....

Here is an example of how my data is setup

categoryList : [
           {
             category: 'Category Title',
             questionList: [
                      {
                         question: 'Question',
                         answer: '
copy goes here
'
                       }, ...
                     ]
            },
            {
               category: 'Next Title',
               questionList: [
                       {
                         question: 'Question',
                         answer: '
copy goes here
'
                       },
                     ]
             }, ...

I was looking at another stackoverflow solution and I really could not figure out how to have it function with my multiple nested arrays. Here is the html:

{{ qa.question }}

and my computed function:

filteredList() {
      for (i = 0; i < this.categoryList.length; i++) {

            var list =  this.categoryList[i];

            for (j = 0; j < list.questionList.length; j++ ) {

                    return list.questionList.filter(qa => {
                                 return qa.question.toLowerCase().includes(this.search.toLowerCase());
                        })
                   }
              }
          }

I'm not sure if I am even close to the correct solution or waaaay off here... please help!


问:
How to redirect to another HTML file with GET variables in Electron?
I'm trying to make an Electron app. In files, in example I have app.html, and I need to send GET variable like ?id=54&q=asd to the receiver.html, and then I'll get this id and q variables on the receiver.html.

Everybody knows, in PHP we do it like that:

app.html:

```<code><a href="receiver.php?id=54&q=asd">Click Me</a>
</code>```

receiver.php:

```<code><?php
echo $_GET['id']." ".$_GET['q'];
?>
</code>```

In electron we redirects pages as this Stackoverflow issue suggested.

But how can I get the GET variables in Electron.js?

答:
You can use the URLSearchParams class to parse the URL and get any query variables with the .get function, like this:

```<code>const queryVariables = new URLSearchParams(window.location.search)
console.log(queryVariables.get('id') + " " + queryVariables.get('q'));
</code>```

I hope this helps.

Body:
I'm trying to make an Electron app. In files, in example I have app.html, and I need to send GET variable like ?id=54&q=asd to the receiver.html, and then I'll get this id and q variables on the receiver.html.

Everybody knows, in PHP we do it like that:

app.html:

Click Me

receiver.php:

<?php
echo $_GET['id']." ".$_GET['q'];
?>

In electron we redirects pages as this Stackoverflow issue suggested.

But how can I get the GET variables in Electron.js?

Body:
You can use the URLSearchParams class to parse the URL and get any query variables with the .get function, like this:

const queryVariables = new URLSearchParams(window.location.search)
console.log(queryVariables.get('id') + " " + queryVariables.get('q'));

I hope this helps.


问:
Fastest way to convert a number to radix 64 in JavaScript?
In JavaScript you can convert a number to a string representation with a specific radix as follows:

```<code>(12345).toString(36) // "9ix"
</code>```

...and you can convert it back to a regular number like this:

```<code>parseInt("9ix", 36) // 12345
</code>```

36 is the highest radix you can specify. It apparently uses the characters 0-9 and a-z for the digits (36 total).

My question: what's the fastest way to convert a number to a base 64 representation (for example, using A-Z, and - and _ for the extra 28 digits)?

Update: Four people have posted responses saying this question is duplicated, or that I'm looking for Base64. I'm not.

"Base64" is a way of encoding binary data in a simple ASCII character set, to make it safe for transfer over networks etc. (so that text-only systems won't garble the binary).

That's not what I'm asking about. I'm asking about converting numbers to a radix 64 string representation. (JavaScript's toString(radix) does this automatically for any radix up to 36; I need a custom function to get radix 64.)

Update 2: Here are some input & output examples...

```<code>0   → "0"
1   → "1"
9   → "9"
10  → "a"
35  → "z"
61  → "Z"
62  → "-"
63  → "_"
64  → "10"
65  → "11"
128 → "20"
etc.
</code>```

答:
Well, you could just use any Javascript Base64 library: perhaps this question answers it?

EDIT: Binary data is essentially just a sequence of bytes. If you assume the bytes represent a single number you can represent the sequence of bytes as a base 64 string. Decode them and do some trivial math on the bytes to get a number. Convert the number to a sequence of bytes and encode to get a string. Seems quite reasonable, unless you are somehow invested in the specific characters used in the String.

ContentLicense: CC BY-SA 3.0

Body:
In JavaScript you can convert a number to a string representation with a specific radix as follows:

(12345).toString(36) // "9ix"

...and you can convert it back to a regular number like this:

parseInt("9ix", 36) // 12345

36 is the highest radix you can specify. It apparently uses the characters 0-9 and a-z for the digits (36 total).

My question: what's the fastest way to convert a number to a base 64 representation (for example, using A-Z, and - and _ for the extra 28 digits)?

Update: Four people have posted responses saying this question is duplicated, or that I'm looking for Base64. I'm not.

"Base64" is a way of encoding binary data in a simple ASCII character set, to make it safe for transfer over networks etc. (so that text-only systems won't garble the binary).

That's not what I'm asking about. I'm asking about converting numbers to a radix 64 string representation. (JavaScript's toString(radix) does this automatically for any radix up to 36; I need a custom function to get radix 64.)

Update 2: Here are some input & output examples...

0   → "0"
1   → "1"
9   → "9"
10  → "a"
35  → "z"
61  → "Z"
62  → "-"
63  → "_"
64  → "10"
65  → "11"
128 → "20"
etc.


问:
Recursive depth function on an array
I have an array of objects like this input, and I want to nest some objects inside another objects (based if their parentId is the parents' forumId),

I got the function working but up to 1 depth, how can I get it working for n depth? Any idea or optimizations are appreciated!

EDIT: After pointing out, the input isn't necessarily ordered.

const input = [
  {
    forumId: 1,
    parentId: null,
    forumName: "Main",
    forumDescription: "",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 2,
    parentId: 1,
    forumName: "Announcements",
    forumDescription: "Announcements & Projects posted here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 3,
    parentId: 1,
    forumName: "General",
    forumDescription: "General forum, talk whatever you want here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 4,
    parentId: 3,
    forumName: "Introduction",
    forumDescription: "A warming introduction for newcomers here",
    forumLocked: false,
    forumDisplay: true,
  },
];

function processInput(forumInput) {
  const topLevelForums = forumInput.filter(function (forum) {
    return forum.parentId === null;
  });

  let output = topLevelForums;

  forumInput.forEach(function (forum) {
    if (forum.parentId !== null) {
      const forumParentId = forum.parentId;
      output.forEach(function (parentForum, idx) {
        if (parentForum.forumId === forumParentId) {
          if (!output[idx].hasOwnProperty("subForums")) {
            output[idx].subForums = [];
          }
          parentForum.subForums.push(forum);
        }
      });
    }
  });

  return output;
}
```

<p>This is the <strong>expected output</strong>:</p>

```[
  {
    forumId: 1,
    parentId: null,
    forumName: "Main",
    forumDescription: "",
    forumLocked: false,
    forumDisplay: true,
    subForums: [
      {
        forumId: 2,
        parentId: 1,
        forumName: "Announcements",
        forumDescription: "Announcements & Projects posted here",
        forumLocked: false,
        forumDisplay: true,
      },
      {
        forumId: 3,
        parentId: 1,
        forumName: "General",
        forumDescription: "General forum, talk whatever you want here",
        forumLocked: false,
        forumDisplay: true,
        subForums: [
          {
            forumId: 4,
            parentId: 3,
            forumName: "Introduction",
            forumDescription: "A warming introduction for newcomers here",
            forumLocked: false,
            forumDisplay: true,
          },
        ],
      },
    ],
  },
]
```

<p>This is the <strong>current output</strong>:</p>

```[
  {
    forumDescription: "",
    forumDisplay: true,
    forumId: 1,
    forumLocked: false,
    forumName: "Main",
    parentId: null,
    subForums: [
      {
        forumDescription: "Announcements & Projects posted here",
        forumDisplay: true,
        forumId: 2,
        forumLocked: false,
        forumName: "Announcements",
        parentId: 1,
      },
      {
        forumDescription: "General forum, talk whatever you want here",
        forumDisplay: true,
        forumId: 3,
        forumLocked: false,
        forumName: "General",
        parentId: 1,
      },
    ],
  },
]
```

答:
Since parents might be after the children in the input, I think I'd approach it by building a Map of forums by ID, then adding the children to them:

```<code>function processInput(forumInput) {
    // Get a map of forums by ID
    const forumsById = new Map();
    for (const forum of forumInput) {
        forumsById.set(forum.forumId, forum);
    }

    // Add child forums to their parents
    for (const forum of forumInput) {
        const {parentId} = forum;
        if (parentId !== null) {
            const parent = forumsById.get(forum.parentId);
            parent.subForums = parent.subForums || []; // Or you could use `?? []` now
            parent.subForums.push(forum);
        }
    }

    // Return the parents array
    return [...forumsById.values()].filter(({parentId}) => parentId === null);
}
</code>```

Live Example:

const input = [
  {
    forumId: 1,
    parentId: null,
    forumName: "Main",
    forumDescription: "",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 2,
    parentId: 1,
    forumName: "Announcements",
    forumDescription: "Announcements & Projects posted here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 3,
    parentId: 1,
    forumName: "General",
    forumDescription: "General forum, talk whatever you want here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 4,
    parentId: 3,
    forumName: "Introduction",
    forumDescription: "A warming introduction for newcomers here",
    forumLocked: false,
    forumDisplay: true,
  },
];

function processInput(forumInput) {
    // Get a map of forums by ID
    const forumsById = new Map();
    for (const forum of forumInput) {
        forumsById.set(forum.forumId, forum);
    }

    // Add child forums to their parents
    for (const forum of forumInput) {
        const {parentId} = forum;
        if (parentId !== null) {
            const parent = forumsById.get(forum.parentId);
            parent.subForums = parent.subForums || []; // Or you could use `?? []` now
            parent.subForums.push(forum);
        }
    }

    // Return the parents array
    return [...forumsById.values()].filter(({parentId}) => parentId === null);
}

console.log(processInput(input));```
<pre class="snippet-code-css lang-css prettyprint-override"><code>.as-console-wrapper {
    max-height: 100% !important;
}</code>```

Note that the above will throw an error if a forum claims to be in a parent forum that isn't in the input.

Title: Recursive depth function on an array

Body:
I have an array of objects like this input, and I want to nest some objects inside another objects (based if their parentId is the parents' forumId),

I got the function working but up to 1 depth, how can I get it working for n depth? Any idea or optimizations are appreciated!

EDIT: After pointing out, the input isn't necessarily ordered.

const input = [
  {
    forumId: 1,
    parentId: null,
    forumName: "Main",
    forumDescription: "",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 2,
    parentId: 1,
    forumName: "Announcements",
    forumDescription: "Announcements & Projects posted here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 3,
    parentId: 1,
    forumName: "General",
    forumDescription: "General forum, talk whatever you want here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 4,
    parentId: 3,
    forumName: "Introduction",
    forumDescription: "A warming introduction for newcomers here",
    forumLocked: false,
    forumDisplay: true,
  },
];

function processInput(forumInput) {
  const topLevelForums = forumInput.filter(function (forum) {
    return forum.parentId === null;
  });

  let output = topLevelForums;

  forumInput.forEach(function (forum) {
    if (forum.parentId !== null) {
      const forumParentId = forum.parentId;
      output.forEach(function (parentForum, idx) {
        if (parentForum.forumId === forumParentId) {
          if (!output[idx].hasOwnProperty("subForums")) {
            output[idx].subForums = [];
          }
          parentForum.subForums.push(forum);
        }
      });
    }
  });

  return output;
}

This is the expected output:

[
  {
    forumId: 1,
    parentId: null,
    forumName: "Main",
    forumDescription: "",
    forumLocked: false,
    forumDisplay: true,
    subForums: [
      {
        forumId: 2,
        parentId: 1,
        forumName: "Announcements",
        forumDescription: "Announcements & Projects posted here",
        forumLocked: false,
        forumDisplay: true,
      },
      {
        forumId: 3,
        parentId: 1,
        forumName: "General",
        forumDescription: "General forum, talk whatever you want here",
        forumLocked: false,
        forumDisplay: true,
        subForums: [
          {
            forumId: 4,
            parentId: 3,
            forumName: "Introduction",
            forumDescription: "A warming introduction for newcomers here",
            forumLocked: false,
            forumDisplay: true,
          },
        ],
      },
    ],
  },
]

This is the current output:

[
  {
    forumDescription: "",
    forumDisplay: true,
    forumId: 1,
    forumLocked: false,
    forumName: "Main",
    parentId: null,
    subForums: [
      {
        forumDescription: "Announcements & Projects posted here",
        forumDisplay: true,
        forumId: 2,
        forumLocked: false,
        forumName: "Announcements",
        parentId: 1,
      },
      {
        forumDescription: "General forum, talk whatever you want here",
        forumDisplay: true,
        forumId: 3,
        forumLocked: false,
        forumName: "General",
        parentId: 1,
      },
    ],
  },
]

Body:
Since parents might be after the children in the input, I think I'd approach it by building a Map of forums by ID, then adding the children to them:

function processInput(forumInput) {
    // Get a map of forums by ID
    const forumsById = new Map();
    for (const forum of forumInput) {
        forumsById.set(forum.forumId, forum);
    }

    // Add child forums to their parents
    for (const forum of forumInput) {
        const {parentId} = forum;
        if (parentId !== null) {
            const parent = forumsById.get(forum.parentId);
            parent.subForums = parent.subForums || []; // Or you could use `?? []` now
            parent.subForums.push(forum);
        }
    }

    // Return the parents array
    return [...forumsById.values()].filter(({parentId}) => parentId === null);
}

Live Example:

const input = [
  {
    forumId: 1,
    parentId: null,
    forumName: "Main",
    forumDescription: "",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 2,
    parentId: 1,
    forumName: "Announcements",
    forumDescription: "Announcements & Projects posted here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 3,
    parentId: 1,
    forumName: "General",
    forumDescription: "General forum, talk whatever you want here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 4,
    parentId: 3,
    forumName: "Introduction",
    forumDescription: "A warming introduction for newcomers here",
    forumLocked: false,
    forumDisplay: true,
  },
];

function processInput(forumInput) {
    // Get a map of forums by ID
    const forumsById = new Map();
    for (const forum of forumInput) {
        forumsById.set(forum.forumId, forum);
    }

    // Add child forums to their parents
    for (const forum of forumInput) {
        const {parentId} = forum;
        if (parentId !== null) {
            const parent = forumsById.get(forum.parentId);
            parent.subForums = parent.subForums || []; // Or you could use `?? []` now
            parent.subForums.push(forum);
        }
    }

    // Return the parents array
    return [...forumsById.values()].filter(({parentId}) => parentId === null);
}

console.log(processInput(input));

.as-console-wrapper {
    max-height: 100% !important;
}

Note that the above will throw an error if a forum claims to be in a parent forum that isn't in the input.


问:
Issues when trying to space XMLHttpRequest() API calls every 5 seconds with setTimeout()
I am working on the initial data pull for a project. It will be cached and further requests will be much less.  I am attempting to space out my API requests every 5 seconds to avoid overloading the server and hitting the rate limit per their rules.  It does not seem that my setTimeout() function is actually working as desired. I logged time stamps to see and all iterations seem to be happening at the exact same time.  I think this somehow due to the asynchronous nature of javascript, but I'm not sure how to remedy it.  Any help would be greatly appreciated.

Code Excerpt:

```<code>var leagues;
function getLeagues(){

    var reqURL = "url";
    var data = new XMLHttpRequest();
    data.open("GET", reqURL);
    data.responseType = "text";
    data.send();

    // parse data on load
    data.onload = function() {
        // League IDs
        leagues = JSON.parse(data.response)

        for (i = 0; i < 5; i++) {
            delay(i); // iterate every 5 secs
        }
    };
}

function delay(i) {
  setTimeout(() => {
    var d = new Date();
    console.log(d.getSeconds())
  }, 5000);
}
</code>```

答:
You can use a Promise instead of a timeout. The setTimeout function will wait five seconds and all code will be executed, because it does not block the execution. A promise instead will do that:

```<code>async function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
};
</code>```

You can use the promise inside of an async loop:

```<code>  async function getLeagues(){
        var reqURL = "url";
        var data = new XMLHttpRequest();
        data.open("GET", reqURL);
    data.responseType = "text";
    data.send();

    // parse data on load
    data.onload = function() {
        // League IDs
        leagues = JSON.parse(data.response)
        const delays = [...new Array(5)]; // To have something to loop over

         for await (const d of delays) {
             await delay(5000);
          }
    };
}
</code>```

Body:
I am working on the initial data pull for a project. It will be cached and further requests will be much less.  I am attempting to space out my API requests every 5 seconds to avoid overloading the server and hitting the rate limit per their rules.  It does not seem that my setTimeout() function is actually working as desired. I logged time stamps to see and all iterations seem to be happening at the exact same time.  I think this somehow due to the asynchronous nature of javascript, but I'm not sure how to remedy it.  Any help would be greatly appreciated.

Code Excerpt:

var leagues;
function getLeagues(){

    var reqURL = "url";
    var data = new XMLHttpRequest();
    data.open("GET", reqURL);
    data.responseType = "text";
    data.send();

    // parse data on load
    data.onload = function() {
        // League IDs
        leagues = JSON.parse(data.response)

        for (i = 0; i < 5; i++) {
            delay(i); // iterate every 5 secs
        }
    };
}

function delay(i) {
  setTimeout(() => {
    var d = new Date();
    console.log(d.getSeconds())
  }, 5000);
}

Body:
You can use a Promise instead of a timeout. The setTimeout function will wait five seconds and all code will be executed, because it does not block the execution. A promise instead will do that:

async function delay(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
};

You can use the promise inside of an async loop:

  async function getLeagues(){
        var reqURL = "url";
        var data = new XMLHttpRequest();
        data.open("GET", reqURL);
    data.responseType = "text";
    data.send();

    // parse data on load
    data.onload = function() {
        // League IDs
        leagues = JSON.parse(data.response)
        const delays = [...new Array(5)]; // To have something to loop over

         for await (const d of delays) {
             await delay(5000);
          }
    };
}


问:
Emojis on textarea does not save post
I have a commenting system and when its just text, no problem - it is saved to the database. When I add a  (for instance), then no comment is saved to the database? Nothing is saving, when there is an emoji.

What can I do to allow emojis?

The "message" is where I am saving the actual comment and where there should be an emoji.

答:
You might want to update the charset and potentially collation. I'm assuming you're using MySQL. This is very confusing, but in MySQL the UTF8 charset isn't actually UTF8, but a mysql's proprietary charset that is largely similar to the actual UTF8, but lacks some characters.
The way to handle it is to switch to the actual UTF8, which in the world of mysql is called utf8mb4_general_ci. You can do so by running

ALTER DATABASE <you db name> CHARACTER SET utf8mb4_general_ci COLLATE utf8mb4_general_ci;
(this will affect only the new tables that you create)

and

ALTER TABLE <you existing table name> CONVERT TO CHARACTER SET utf8mb4_general_ci;
(this will update an already existing table, although the emojis that you already lost cannot be recovered)

Title: Emojis on textarea does not save post


问:
A problem returning a SwiftUI (transformed) Path from a function
I am working on a custom SwiftUI button that has a special shape depending on configuration.

A utility function must return a Shape depending on configuration. Since the path/shape is complicated to programme, transformations are used to get variations depending on configuration.

For demonstration purpose, a simplified version of the utility function looks like this:

```<code>func shape(i: Int) -> some Shape {

        let path = Path()

        switch i {
        case 0:
            return path
        case 1:
            return path.transform(CGAffineTransform(translationX:0,y:0))
        default:
            break
        }

        return path
    }
</code>```

Unfortunately the compiler generate the well known error:

```<code>Function declares an opaque return type, but the return statements in its body do not have matching underlying types
</code>```

I am sure this happens due to the transformation, although it returns a struct

```<code>func transform(_ transform: CGAffineTransform) -> TransformedShape<Self>
</code>```

that conforms to Shape (like Path do):

```<code>struct TransformedShape<Content> where Content : Shape
</code>```

Documentation for Path states that Path conforms to Shape, and this makes me think that I am overlooking something or that this may be a bug in the compiler.

If someone knows a workaround or can see that I am doing something wrong - please let me known.

Thanks.

答:
The problem is that you're promising to return a specific type, but you return two different types: Path and TransformedShape. To fix that, always return TransformedShape by applying CGAffineTransform.identity in cases where you don't want to change the shape. For example in your case:

```<code>func shape(i: Int) -> some Shape {

    let path = Path()

    switch i {
    case 0:
        return path.transform(.identity)
    case 1:
        return path.transform(CGAffineTransform(translationX:0,y:0))
    default:
        break
    }

    return path.transform(.identity)
}
</code>```

Though I would probably write it this way (assuming that there are more cases, and that the zero-translate transform is just for demonstration):

```<code>func shape(i: Int) -> some Shape {

    let transform: CGAffineTransform
    switch i {
    case 1:
        transform = CGAffineTransform(translationX:0, y:0)
    default:
        transform = .identity
    }

    return Path().transform(transform)
}
</code>```

some types do not mean "any type that conforms." They are opaque types, which means "this function returns some specific, known-at-compile-time type, but don't tell the caller what precise type it is."

Body:
I am working on a custom SwiftUI button that has a special shape depending on configuration.

A utility function must return a Shape depending on configuration. Since the path/shape is complicated to programme, transformations are used to get variations depending on configuration.

For demonstration purpose, a simplified version of the utility function looks like this:

func shape(i: Int) -> some Shape {

        let path = Path()

        switch i {
        case 0:
            return path
        case 1:
            return path.transform(CGAffineTransform(translationX:0,y:0))
        default:
            break
        }

        return path
    }

Unfortunately the compiler generate the well known error:

Function declares an opaque return type, but the return statements in its body do not have matching underlying types

I am sure this happens due to the transformation, although it returns a struct

func transform(_ transform: CGAffineTransform) -> TransformedShape<Self>

that conforms to Shape (like Path do):

struct TransformedShape<Content> where Content : Shape

Documentation for Path states that Path conforms to Shape, and this makes me think that I am overlooking something or that this may be a bug in the compiler.

If someone knows a workaround or can see that I am doing something wrong - please let me known.

Thanks.

Body:
The problem is that you're promising to return a specific type, but you return two different types: Path and TransformedShape. To fix that, always return TransformedShape by applying CGAffineTransform.identity in cases where you don't want to change the shape. For example in your case:

func shape(i: Int) -> some Shape {

    let path = Path()

    switch i {
    case 0:
        return path.transform(.identity)
    case 1:
        return path.transform(CGAffineTransform(translationX:0,y:0))
    default:
        break
    }

    return path.transform(.identity)
}

Though I would probably write it this way (assuming that there are more cases, and that the zero-translate transform is just for demonstration):

func shape(i: Int) -> some Shape {

    let transform: CGAffineTransform
    switch i {
    case 1:
        transform = CGAffineTransform(translationX:0, y:0)
    default:
        transform = .identity
    }

    return Path().transform(transform)
}

some types do not mean "any type that conforms." They are opaque types, which means "this function returns some specific, known-at-compile-time type, but don't tell the caller what precise type it is."


问:
Flocking boids behaviour problem
Yesterday I came across Craig Reynolds' Boids, and subsequently figured that I'd give implementing a simple 2D version in Java a go.
I've put together a fairly basic setup based closely on Conrad Parker's notes.

However, I'm getting some rather bizarre (in my opinion) behaviour. Currently, my boids move reasonably quickly into a rough grid or lattice, and proceed to twitch on the spot. By that I mean they move around a little and rotate very frequently.

Currently, I have implemented:

Alignment

Cohesion

Separation

Velocity limiting

Initially, my boids are randomly distributed across the screen area (slightly different to Parker's method), and their velocities are all directed towards the centre of the screen area (note that randomly initialised velocities give the same result). Changing the velocity limit value only changes how quickly the boids move into this pattern, not formation of the pattern.

As I see it, this could be:

A consequence of the parameters I'm using (right now my code is as described in Parker's pseudocode; I have not yet tried areas of influence defined by an angle and a radius as described by Reynolds.)

Something I need to implement but am not aware of.

Something I am doing wrong.

The expected behaviour would be something more along the lines of a two dimensional version of what happens in the applet on Reynolds' boids page, although right now I haven't implemented any way to keep the boids on screen.

Has anyone encountered this before? Any ideas about the cause and/or how to fix it? I can post a .gif of the behaviour in question if it helps.

答:
If they see everyone, they will all try to move with average velocity. If they see only some there can be some separated groups.

And if they are randomly distributed, it will be close to zero.

If you limit them by rectangle and either repulse them from walls or teleport them to other side when they got close) and have too high separation, they will be pushed from walls (from walls itself or from other who just were teleported, who will then be pushed to other side (and push and be pushed again)).

So try tighter cohesion, limited sight, more space and distribute them clustered (pick random point and place multiple of them small random distance from there), not uniformly or normaly.

Title: Flocking boids behaviour problem


问:
npx react-native run-android: E/Device: Error during Sync: EOF
I am trying to run react native app to my real android device.
I checked my device before running

```<code>adb devices

List of devices attached
3357425441473098        device
</code>```

I started with

```<code>npx react-native start
</code>```

and in other console

```<code>npx react-native run-android
</code>```

But got an error..

```<code>Task :app:installDebug FAILED
10:25:40 V/ddms: execute: running am get-config
10:25:40 V/ddms: execute 'am get-config' on '3357425441473098' : EOF hit. Read: -1
10:25:40 V/ddms: execute: returning
Installing APK 'app-debug.apk' on 'SM-G960U - 9' for app:debug
10:25:40 D/app-debug.apk: Uploading app-debug.apk onto device '3357425441473098'
10:25:40 D/Device: Uploading file onto device '3357425441473098'
10:25:40 D/ddms: Reading file permision of /home/user/react-native/awesomeProject/android/app/build/outputs/apk/debug/app-debug.apk as: rw-rw-r--
10:25:40 D/ddms: read: channel EOF
10:25:40 E/Device: Error during Sync: EOF
Unable to install /home/user/react-native/awesomeProject/android/app/build/outputs/apk/debug/app-debug.apk
com.android.ddmlib.InstallException: EOF
</code>```

And right after that error. I lost my device connection.

```<code>adb devices
</code>```

shows nothing..

and at the bottom of stacktrace I see this error also

```<code>Caused by: java.io.IOException: EOF
        at com.android.ddmlib.AdbHelper.read(AdbHelper.java:862)
        at com.android.ddmlib.SyncService.doPushFile(SyncService.java:712)
        at com.android.ddmlib.SyncService.pushFile(SyncService.java:406)
        at com.android.ddmlib.Device.syncPackageToDevice(Device.java:988)
        at com.android.ddmlib.Device.installPackage(Device.java:902)
</code>```

FAILURE: Build failed with an exception.

```<code>* What went wrong:
Execution failed for task ':app:installDebug'.
> com.android.builder.testing.api.DeviceException: com.android.ddmlib.InstallException: EOF

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 10s

    at checkExecSyncError (child_process.js:629:11)
    at execFileSync (child_process.js:647:13)
    at runOnAllDevices (/home/user/react-native/newProject/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevice
s.js:94:39)
    at buildAndRun (/home/user/react-native/newProject/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/index.js:158:41)
    at then.result (/home/user/react-native/newProject/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/index.js:125:12)
    at process._tickCallback (internal/process/next_tick.js:68:7)
</code>```

My device is us samsumg galaxy s9.  It was working when I use Expo sdk.
What is my problem?

react-native : 0.61.5

Weird thing is that it works first execution after computer boot. then I tried reconnect phone, and it pops up again.

答:
Just had the same problem. Turned out it was an issue with my USB cable. For some reason the installation process was being interrupted half way through by the device going offline.
I've replaced the cable, and everything started working again.</p>

Body:
I am trying to run react native app to my real android device.
I checked my device before running

adb devices

List of devices attached
3357425441473098        device

I started with

npx react-native start

and in other console

npx react-native run-android

But got an error..

Task :app:installDebug FAILED
10:25:40 V/ddms: execute: running am get-config
10:25:40 V/ddms: execute 'am get-config' on '3357425441473098' : EOF hit. Read: -1
10:25:40 V/ddms: execute: returning
Installing APK 'app-debug.apk' on 'SM-G960U - 9' for app:debug
10:25:40 D/app-debug.apk: Uploading app-debug.apk onto device '3357425441473098'
10:25:40 D/Device: Uploading file onto device '3357425441473098'
10:25:40 D/ddms: Reading file permision of /home/user/react-native/awesomeProject/android/app/build/outputs/apk/debug/app-debug.apk as: rw-rw-r--
10:25:40 D/ddms: read: channel EOF
10:25:40 E/Device: Error during Sync: EOF
Unable to install /home/user/react-native/awesomeProject/android/app/build/outputs/apk/debug/app-debug.apk
com.android.ddmlib.InstallException: EOF

And right after that error. I lost my device connection.

adb devices

shows nothing..

and at the bottom of stacktrace I see this error also

Caused by: java.io.IOException: EOF
        at com.android.ddmlib.AdbHelper.read(AdbHelper.java:862)
        at com.android.ddmlib.SyncService.doPushFile(SyncService.java:712)
        at com.android.ddmlib.SyncService.pushFile(SyncService.java:406)
        at com.android.ddmlib.Device.syncPackageToDevice(Device.java:988)
        at com.android.ddmlib.Device.installPackage(Device.java:902)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:installDebug'.
> com.android.builder.testing.api.DeviceException: com.android.ddmlib.InstallException: EOF

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 10s

    at checkExecSyncError (child_process.js:629:11)
    at execFileSync (child_process.js:647:13)
    at runOnAllDevices (/home/user/react-native/newProject/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevice
s.js:94:39)
    at buildAndRun (/home/user/react-native/newProject/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/index.js:158:41)
    at then.result (/home/user/react-native/newProject/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/index.js:125:12)
    at process._tickCallback (internal/process/next_tick.js:68:7)

My device is us samsumg galaxy s9.  It was working when I use Expo sdk.
What is my problem?

react-native : 0.61.5

Weird thing is that it works first execution after computer boot. then I tried reconnect phone, and it pops up again.


答:
I think the systematic approach is
1. create a map of id to object
2. create a map of parent -> children
3. add all the parent to the results
4. recursively add children (subforums)</p>

```<code>const input = [
  {
    forumId: 1,
    parentId: null,
    forumName: "Main",
    forumDescription: "",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 2,
    parentId: 1,
    forumName: "Announcements",
    forumDescription: "Announcements & Projects posted here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 3,
    parentId: 1,
    forumName: "General",
    forumDescription: "General forum, talk whatever you want here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 4,
    parentId: 3,
    forumName: "Introduction",
    forumDescription: "A warming introduction for newcomers here",
    forumLocked: false,
    forumDisplay: true,
  },
];

const mapIdToForums = input.reduce((acc, cur) => {
  acc[cur.forumId] = cur;
  return acc;
}, {});

const mapForumsToSubForums = input.reduce((acc, cur) => {
  parentId = cur.parentId || ""; // no parent
  acc[parentId] = acc[parentId] || [];
  acc[parentId].push(cur);
  return acc;
}, {});

const addChildren = (parent) => {
  var children = mapForumsToSubForums[parent.forumId];
  if (children) {
    parent.subForums = children

    children.forEach(c => addChildren(c));
  }
};

results = mapForumsToSubForums[""];
results.forEach(p => addChildren(p));
console.log(results);
</code>```

Title: Recursive depth function on an array

Body:
I think the systematic approach is
1. create a map of id to object
2. create a map of parent -> children
3. add all the parent to the results
4. recursively add children (subforums)

const input = [
  {
    forumId: 1,
    parentId: null,
    forumName: "Main",
    forumDescription: "",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 2,
    parentId: 1,
    forumName: "Announcements",
    forumDescription: "Announcements & Projects posted here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 3,
    parentId: 1,
    forumName: "General",
    forumDescription: "General forum, talk whatever you want here",
    forumLocked: false,
    forumDisplay: true,
  },
  {
    forumId: 4,
    parentId: 3,
    forumName: "Introduction",
    forumDescription: "A warming introduction for newcomers here",
    forumLocked: false,
    forumDisplay: true,
  },
];

const mapIdToForums = input.reduce((acc, cur) => {
  acc[cur.forumId] = cur;
  return acc;
}, {});

const mapForumsToSubForums = input.reduce((acc, cur) => {
  parentId = cur.parentId || ""; // no parent
  acc[parentId] = acc[parentId] || [];
  acc[parentId].push(cur);
  return acc;
}, {});

const addChildren = (parent) => {
  var children = mapForumsToSubForums[parent.forumId];
  if (children) {
    parent.subForums = children

    children.forEach(c => addChildren(c));
  }
};

results = mapForumsToSubForums[""];
results.forEach(p => addChildren(p));
console.log(results);


问:
Why is ONE basic arithmetic operation in for loop body executed SLOWER THAN TWO arithmetic operations?
While I experimented with measuring time of execution of arithmetic operations, I came across very strange behavior. A code block containing a for loop with one arithmetic operation in the loop body was always executed slower than an identical code block, but with two arithmetic operations in the for loop body. Here is the code I ended up testing:

```<code>#include <iostream>
#include <chrono>

#define NUM_ITERATIONS 100000000

int main()
{
    // Block 1: one operation in loop body
    {
        int64_t x = 0, y = 0;
        auto start = std::chrono::high_resolution_clock::now();

        for (long i = 0; i < NUM_ITERATIONS; i++) {x+=31;}

        auto end = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> diff = end-start;
        std::cout << diff.count() << " seconds. x,y = " << x << "," << y << std::endl;
    }

    // Block 2: two operations in loop body
    {
        int64_t x = 0, y = 0;
        auto start = std::chrono::high_resolution_clock::now();

        for (long i = 0; i < NUM_ITERATIONS; i++) {x+=17; y-=37;}

        auto end = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> diff = end-start;
        std::cout << diff.count() << " seconds. x,y = " << x << "," << y << std::endl;
    }

    return 0;
}
</code>```

I tested this with different levels of code optimization (-O0,-O1,-O2,-O3), with different online compilers (for example onlinegdb.com), on my work machine, on my hame PC and laptop, on RaspberryPi and on my colleague's computer. I rearranged these two code blocks, repeated them, changed constants, changed operations (+, -, <<, =, etc.), changed integer types. But I always got similar result: the block with one line in loop is SLOWER than block with two lines:

1.05681 seconds. x,y = 3100000000,0

  0.90414 seconds. x,y = 1700000000,-3700000000

I checked the assembly output on https://godbolt.org/ but everything looked like I expected: second block just had one more operation in assembly output.

Three operations always behaved as expected: they are slower than one and faster than four. So why two operations produce such an anomaly?

Edit:

Let me repeat: I have such behaviour on all of my Windows and Unix machines with code not optimized. I looked at assembly I execute (Visual Studio, Windows) and I see the instructions I want to test there. Anyway if the loop is optimized away, there is nothing I ask about in the code which left. I added that optimizations notice in the question to avoid "do not measure not optimized code" answers because optimizations is not what I ask about. The question is actually why my computers execute two operations faster than one, first of all in code where these operations are not optimized away. The difference in time of execution is 5-25% on my tests (quite noticeable).

答:
@PeterCordes proved this answer to be wrong in many assumptions, but it could still be useful as some blind research attempt of the problem.

I set up some quick benchmarks, thinking it may somehow be connected to code memory alignment, truly a crazy thought.

But it seems that @Adrian McCarthy got it right with the dynamic frequency scaling.

Anyway benchmarks tell that inserting some NOPs could help with the issue, with 15 NOPs after the x+=31 in Block 1 leading to nearly the same performance as the Block 2. Truly mind blowing how 15 NOPs in single instruction loop body increase performance.

http://quick-bench.com/Q_7HY838oK5LEPFt-tfie0wy4uA

I also tried -OFast thinking compilers might be smart enough to throw away some code memory inserting such NOPs, but it seems not to be the case.
http://quick-bench.com/so2CnM_kZj2QEWJmNO2mtDP9ZX0

Edit: Thanks to @PeterCordes it was made clear that optimizations were never working quite as expected in benchmarks above (as global variable required add instructions to access memory), new benchmark http://quick-bench.com/HmmwsLmotRiW9xkNWDjlOxOTShE clearly shows that Block 1 and Block 2 performance is equal for stack variables. But NOPs could still help with single-threaded application with loop accessing global variable, which you probably should not use in that case and just assign global variable to local variable after the loop.

Edit 2: Actually optimizations never worked due to quick-benchmark macros making variable access volatile, preventing important optimizations. It is only logical to load the variable once as we are only modifying it in the loop, so it is volatile or disabled optimizations being the bottleneck. So this answer is basically wrong, but at least it shows how NOPs could speed-up unoptimized code execution, if it makes any sense in the real world (there are better ways like bucketing counters).

Body:
While I experimented with measuring time of execution of arithmetic operations, I came across very strange behavior. A code block containing a for loop with one arithmetic operation in the loop body was always executed slower than an identical code block, but with two arithmetic operations in the for loop body. Here is the code I ended up testing:

#include <iostream>
#include <chrono>

#define NUM_ITERATIONS 100000000

int main()
{
    // Block 1: one operation in loop body
    {
        int64_t x = 0, y = 0;
        auto start = std::chrono::high_resolution_clock::now();

        for (long i = 0; i < NUM_ITERATIONS; i++) {x+=31;}

        auto end = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> diff = end-start;
        std::cout << diff.count() << " seconds. x,y = " << x << "," << y << std::endl;
    }

    // Block 2: two operations in loop body
    {
        int64_t x = 0, y = 0;
        auto start = std::chrono::high_resolution_clock::now();

        for (long i = 0; i < NUM_ITERATIONS; i++) {x+=17; y-=37;}

        auto end = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> diff = end-start;
        std::cout << diff.count() << " seconds. x,y = " << x << "," << y << std::endl;
    }

    return 0;
}

I tested this with different levels of code optimization (-O0,-O1,-O2,-O3), with different online compilers (for example onlinegdb.com), on my work machine, on my hame PC and laptop, on RaspberryPi and on my colleague's computer. I rearranged these two code blocks, repeated them, changed constants, changed operations (+, -, <<, =, etc.), changed integer types. But I always got similar result: the block with one line in loop is SLOWER than block with two lines:

1.05681 seconds. x,y = 3100000000,0

  0.90414 seconds. x,y = 1700000000,-3700000000

I checked the assembly output on https://godbolt.org/ but everything looked like I expected: second block just had one more operation in assembly output.

Three operations always behaved as expected: they are slower than one and faster than four. So why two operations produce such an anomaly?

Edit:

Let me repeat: I have such behaviour on all of my Windows and Unix machines with code not optimized. I looked at assembly I execute (Visual Studio, Windows) and I see the instructions I want to test there. Anyway if the loop is optimized away, there is nothing I ask about in the code which left. I added that optimizations notice in the question to avoid "do not measure not optimized code" answers because optimizations is not what I ask about. The question is actually why my computers execute two operations faster than one, first of all in code where these operations are not optimized away. The difference in time of execution is 5-25% on my tests (quite noticeable).


问:
mvc controller test with session attribute
I'm trying to test a method with this signature:

```<code> @Autowired
HttpSession http_Session;
@RequestMapping(method=RequestMethod.GET, value="/search/findByName")
public @ResponseBody List<Map> search(@RequestParam(value="name", required=true) String name){
Integer user_id = http_Session.getAttribute("userinfo");
}
</code>```

userinfo is a class which contains informations about the user and set in session scope when the user logged in.but when I try the test :

```<code>@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {
   "classpath:/META-INF/applicationContext.xml"})

 public class userControllerTest {

private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
 }
 @Test
 public void userTest() throws Exception {
     mockMvc.perform(MockMvcRequestBuilders.get("/search/findByName").param("name", "bob"))
          .andDo(print())
          .andExpect(MockMvcResultMatchers.status().isOk());
    }
</code>```

The problem is the userinfo class attribute is set in another method so when i try to access it in this method  i got a NullPointerException , and with Autowiring the httpSession i got a new Session for each method   i have to test.

What should i do with the session attribute, my method doesn't accept a session parameter , and for each test it create a WebApplicationContext with a new session.

答:
You could also share a session across different requests:

```<code>import static org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.sharedHttpSession;

@Before
public void setUp() {
    this.mockMvc = MockMvcBuilders
    .webAppContextSetup(this.webApplicationContext)
    .apply(sharedHttpSession()) // use this session across requests
    .build();
}
</code>```

note: this session will be shared among requests performed against the same MockMvc instance only.

Body:
I'm trying to test a method with this signature:

 @Autowired
HttpSession http_Session;
@RequestMapping(method=RequestMethod.GET, value="/search/findByName")
public @ResponseBody List<Map> search(@RequestParam(value="name", required=true) String name){
Integer user_id = http_Session.getAttribute("userinfo");
}

userinfo is a class which contains informations about the user and set in session scope when the user logged in.but when I try the test :

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {
   "classpath:/META-INF/applicationContext.xml"})

 public class userControllerTest {

private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
 }
 @Test
 public void userTest() throws Exception {
     mockMvc.perform(MockMvcRequestBuilders.get("/search/findByName").param("name", "bob"))
          .andDo(print())
          .andExpect(MockMvcResultMatchers.status().isOk());
    }

The problem is the userinfo class attribute is set in another method so when i try to access it in this method  i got a NullPointerException , and with Autowiring the httpSession i got a new Session for each method   i have to test.

What should i do with the session attribute, my method doesn't accept a session parameter , and for each test it create a WebApplicationContext with a new session.

Body:
You could also share a session across different requests:

import static org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.sharedHttpSession;

@Before
public void setUp() {
    this.mockMvc = MockMvcBuilders
    .webAppContextSetup(this.webApplicationContext)
    .apply(sharedHttpSession()) // use this session across requests
    .build();
}

note: this session will be shared among requests performed against the same MockMvc instance only.


问:
Angular 8 : How to send the value from *ngfor to TS file
I have a code

```<code><div *ngFor="let a of ABC">
{{a.error}}
</div>
</code>```

Now I want to send the value of a to my TS file. I want to meet some expectation say 'a.error == true' then call some function and assign the value of a to some variable in ts file.

I hope it is clear. please ask if require more clarification

答:
You should probably loop the variable ABC in the TS file before exposing it to the html template. Be aware of the fact that. if you call a function in the html, it will be evaluated many many times.

e.g. , the following will execute the someFunction many more times than you probably need.

```<code>//.html
<div *ngFor="let a of ABC">
{{someFunction(a)}}
</div>
</code>```

I suggest creating the values you need to read in the html as a different var.
for example.

```<code>//.ts
ERR: string[];
ngOnInit() {
  this.ERR = ABC.map(a => someFunction(a));
}
</code>```

Then in the template just bind to the error array

```<code>//.html
<div *ngFor="let a of ERR">
    {{a}}
</div>
</code>```

Body:
I have a code

{{a.error}}

Now I want to send the value of a to my TS file. I want to meet some expectation say 'a.error == true' then call some function and assign the value of a to some variable in ts file.

I hope it is clear. please ask if require more clarification

Body:
You should probably loop the variable ABC in the TS file before exposing it to the html template. Be aware of the fact that. if you call a function in the html, it will be evaluated many many times.

e.g. , the following will execute the someFunction many more times than you probably need.

//.html
<div *ngFor="let a of ABC">
{{someFunction(a)}}
</div>

I suggest creating the values you need to read in the html as a different var.
for example.

//.ts
ERR: string[];
ngOnInit() {
  this.ERR = ABC.map(a => someFunction(a));
}

Then in the template just bind to the error array

//.html
<div *ngFor="let a of ERR">
    {{a}}
</div>